I am looking to update the color scheme of menu elements as the page scrolls using the provided template. I tried a few methods, but they lack dynamism. How can I make it more dynamic?
$(document).ready(function(){
//scroll animation
$('.navigation a').on('click',function(){
var target = $(this);
var element = target.attr('href');
$('.navigation a').removeClass('active');
target.addClass('active');
navHeight = $(".navigation").height();
$("body, html").animate({
scrollTop: $( element ).offset().top + navHeight - 120
}, 800);
});
//updating menu element colors while scrolling
$(window).scroll(function() {
var scrollTop = $(document).scrollTop();
if(scrollTop < 1000) {
$(".navigation a").removeClass('active');
$("a[title='About']").addClass('active');
}
else if(scrollTop > 1001 && scrollTop < 2000) {
$(".navigation a").removeClass('active');
$("a[title='Work']").addClass('active');
}
else if(scrollTop > 2001 && scrollTop < 3000) {
$(".navigation a").removeClass('active');
$("a[title='Clients']").addClass('active');
}
else if(scrollTop > 3001 && scrollTop < 4200) {
$(".navigation a").removeClass('active');
$("a[title='Blogs']").addClass('active');
}
else if(scrollTop > 4200 && scrollTop < 4600) {
$(".navigation a").removeClass('active');
$("a[title='Contact']").addClass('active');
}
});
});
* {
list-style-type: none;
padding: 0;
margin: 0;
}
body {
font-size: 16px;
background: #eee;
padding-top: 90px;
font-family: 'Roboto',Arial, Helvetica, Sans-serif;
overflow-x: hidden;
}
.navigation {
top: 0;
left: 0;
padding: 0 10%;
width: 100%;
position: fixed;
color: #fff;
box-sizing: border-box;
background: #363636;
text-align: center;
}
.navigation a {
color: inherit;
margin: 35px 5px;
line-height: 150%;
padding: 0 5px 0 20px;
display: inline-block;
text-decoration: none;
border-left: 1px solid #fff;
}
.navigation a.active { color: yellow;}
.navigation a:first-child { border: 0; }
.box {
width: 100vw;
height: 100vh;
font-size: 14vw;
padding-top: 100px;
text-align: center;
background: #4CD2DA;
}
.box:nth-child(2) { background: #59DAE2; }
.box:nth-child(3) { background: #4372A6; }
.box:nth-child(4) { background: #D8E0E3; }
.box:nth-child(5) { background: #4B565A; }
<html>
<head>
<title>ScrollSpy</title>
<link rel="stylesheet" type="text/css" href="scrollspy.css">
</head>
<body>
<nav class="navigation">
<a class="active" href="#about" title="About">About</a>
<a href="#work" title="Work">Work</a>
<a href="#clients" title="Clients">Clients</a>
<a href="#blogs" title="Blogs">Blogs</a>
<a href="#contact" title="Contact">Contact</a>
</nav>
<div id="container">
<div id="about" class="box">
About
</div>
<div id="work" class="box"gt;
Work
</div>
<div id="clients" class="box">
Clients
</div>
<div id="blogs" class="box">
Blogs
</div>
<div id="contact" class="box">
Contact
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
<script type="text/javascript" src="scrollspy.js"></script>
</body>
</html>