On my current site(uptrde.com) I have it setup to when you scroll to a certain section of the page the nav a that corresponds with that section lights up. However, I feel that the way I am doing it with jQuery is difficult and not modular at all. My code for animating the page scrolls works fine, just this last bit is giving me trouble. I would like to make this code more modular so if I add more sections to the site in the future I don't have to figure out new values. Thank you for your help.
$(window).scroll(function() {
$(".mainNav a").removeClass("selected");
var scroll = $(window).scrollTop();
//add the 'selected' class to the correct id element based on the scroll amount
if (scroll >= 0 && scroll <= 855) {
$("#homeNav").addClass("selected");
} else if (scroll >= 855 && scroll <= 1808) {
$("#aboutNav").addClass("selected");
} else if (scroll >= 1808 && scroll <= 2700) {
$("#sampleNav").addClass("selected");
} else if (scroll >= 2700 && scroll <= 3780){
$("#clientsNav").addClass("selected");
} else {
$("#contactNav").addClass("selected");
}
});
<nav id="menu">
<ul class="mainNav">
<li><a href="#top" id="homeNav" class="selected">Home</a></li>
<li><a href="#about-header" id="aboutNav">About Us</a></li>
<li><a href="#samples-header" id="sampleNav">Samples</a></li>
<li><a href="#clients-header" id="clientsNav">Clients</a></li>
<li><a href="#contact-header" id="contactNav">Contact</a></li>
</ul>
</nav>