Changing CSS of one element when it's hovering another

Luiz Cruz

I have a fixed position navbar with white links and I'm trying to make the links turn to black when they're above ("hovering") another element with a white background.

Navbar links CSS:

nav ul li a {color:#ffffff;}

Navbar CSS:

nav {position:fixed;}

White div CSS:

div.white {background-color:#ffffff;}

Imagine this is the HTML:

<nav><ul><li><a>Link</a></li></ul></nav>
<!-- Imagine there's a hero image here -->
<div id="heroimage"></div>
<div class="white">

Since the navbar is fixed to the top, when you scroll the website, the navbar will eventually get on top ("hover") the white div and the links will become "invisible". I'm trying to find a way to make them turn to black only when they are on top of the white div.

Is this possible with JQuery? What functions am I looking for? I couldn't find an answer elsewhere...

Almond

Okay, see if that is more what you were asking for. You will have to scroll to see the "NAVBAR" color change. Hope this helps. Let me know if you have any questions.

Edit:

So now that I understand what you meant.

Here's another Fiddle. https://jsfiddle.net/u96xa39L/2/

HTML:

<div id="colorMeDifferent" class="blackbarwhitelinks">NAVBAR</div>

<div class="hugeDiv">No color change on overlap</div>

<div id="hoveroverme" class="hoverBar">When we over lap here, colors change</div>

<div class="hugeDiv">No color change on overlap</div>

Jquery/Javascript

$(window).scroll(function() {
  var hoverElement = $("#hoveroverme");
  var elementsToChange = $("#colorMeDifferent");


  if ( Math.abs(hoverElement.offset().top - elementsToChange.offset().top)  <= hoverElement.height()) {
    elementsToChange[0].className = "whiteBarBlackLinks";       
  } else {
    elementsToChange[0].className = "blackbarwhitelinks";
  }
});

CSS

#colorMeDifferent {
  position: fixed;
}

.blackbarwhitelinks {
  background-color: black;
  color: white;
}

.whiteBarBlackLinks {
  background-color: white;
  color:black;
}

.hugeDiv {
  min-height: 650px;
  background-color:red;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Changing the child element's CSS when the parent is hovered

How to change color of element when hovering over another element?

Changing the marker color when hovering on corresponding element -> Angular 4

How to change CSS elements of an element when hovering on another

CSS Hover Opacity Transition flicker when hovering over element that is on top of another element

WPF - hide element when hovering a mouse over another element

Hovering one element changes another in the loop JS/jquery

Change css class when hovering html element

jQuery - need to toggle one element when hovering another element

Changing the child element's color but only when hovering specific parent element (all parents have the same class)

Changing color of letters one by one in a html text when hovering

CSS - affecting an element by hovering on another one, when they both belong to different divs

Change animation's transition speed in CSS when un-hovering element

Hovering over one element changes another?

I need to show a element in a table when hovering another next to it with javascript

How to show one element when click on the another element with css?

How to change css of one class when hovering over another?

Make an element appear and make adjustments when hovering over another element

Changing the style of one div when hovering over another

Trying to get div element to move while hovering another one

css transition - issue when hovering on the element

Display DIV when hovering another DIV's child CSS

Affect another div when hovering CSS

change css of one div by hovering on another div

How to get a changing height value of one element and return it as a CSS "top" value on another element in jQuery?

display an element when hovering on another element

How can I change one svg path ID element when hovering over another element

Changing Option Value Of Another Element With Javascript When Another Select Element's Option Is Changed

While hovering over one element, disable hover on another