Reputation: 2711
I need the nav bar to be white when scrolling over the blue section only. I have tried to do this myself but it doesn't work properly. This is not the end result I am trying to achieve I just need this to work so that I can continue coding it. thanks
https://codepen.io/Reece_Dev/pen/MmKprq
$(window).scroll(function() {
var scrollPos = $(window).scrollTop();
var page1Top = $("#sec_one").scrollTop();
var page1Bot = $("#sec_one").outerHeight();
var page2 = $("#sec_two").scrollTop();
var page3 = $("#sec_three").scrollTop();
if (scrollPos => page1Top && scrollPos < page1Bot) {
$('nav').css("background-color", "#ffffff");
}
});
* {
margin: 0;
padding: 0;
}
nav {
width: 100%;
background-color: black;
position: fixed;
top: 0;
}
nav ul {
width: 50%;
margin: 0 auto;
list-style-type: none;
text-align: center;
}
nav ul li {
display: inline;
width: 100%;
}
nav ul li a {
font-size: 40px;
color: white;
text-decoration: none;
}
nav ul li a {}
.sections {
width: 100%;
height: 100vh;
}
#sec_one {
background-color: blue;
}
#sec_two {
background-color: red;
}
#sec_three {
background-color: yellow;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<nav>
<ul>
<li><a href="" id="link 1">Link 1</a></li>
<li><a href="" id="link 2">Link 2</a></li>
<li><a href="" id="link 3">Link 3</a></li>
</ul>
</nav>
<div id="sec_one" class="sections">
</div>
<div id="sec_two" class="sections">
</div>
<div id="sec_three" class="sections">
</div>
Upvotes: 1
Views: 317
Reputation: 764
It's because "superior or equal" needs to be written this way in JavaScript: >=
. Yours is like this: =>
.
And you need to change back the color if the scroll position condition is false:
if(scrollPos >= page1Top && scrollPos < page1Bot){
$('nav').css("background-color", "#ffffff");
} else {
$('nav').css("background-color", "#000000");
}
If the blue section is the first one at the top of the page, you should define it to white in the CSS code.
See working JSFiddle.
Upvotes: 3