I'm having a problem with the side nav on my site (desktop view): https://www.teamwinstudios.com
I want the active class on the side nav to inherit the active class as soon as (or closer to) the relevant page comes into view as the user is scrolling down. So for example, if you scroll down you'll notice the side nav changes from 'Home' to 'Featured Work' at around the middle of the image grid of the 'Featured Work' page. What I want is for this change to happen as the 'Featured Works' title comes into view. The active class needs to change earlier.
This is the relevant html for the side nav:
<nav class="Index-nav">
<div class="Index-nav-inner">
<a href="#home2" class="Index-nav-item active">
<div class="Index-nav-indicator"></div>
<div class="Index-nav-text"><span>Home</span></div>
</a>
<a href="#recent-work" class="Index-nav-item">
<div class="Index-nav-indicator"></div>
<div class="Index-nav-text"><span>Featured Work</span></div>
</a>
<a href="#team-win" class="Index-nav-item">
<div class="Index-nav-indicator"></div>
<div class="Index-nav-text"><span>Who We Are</span></div>
</a>
<a href="#services" class="Index-nav-item">
<div class="Index-nav-indicator"></div>
<div class="Index-nav-text"><span>What We Do</span></div>
</a>
<a href="#contact-1" class="Index-nav-item">
<div class="Index-nav-indicator"></div>
<div class="Index-nav-text"><span>Contact</span></div>
</a>
</div>
</nav>
I've used jQuery below to fade out the active class:
<script>
$(document).ready(function() {
$(document).scroll(function() {
var y = $(this).scrollTop();
if (y > 400) {
$('.Index-nav-item.active, .Index-nav-text').fadeOut();
} else {
$('.Index-nav-item.active, .Index-nav-text').fadeIn();
}
});
});
</script>
I'm stuck now targeting the the next item from index-nav-text
to fade in and inherit the class active; w This is what I've tried:
<script>
$(document).ready(function() {
$(document).scroll(function() {
var y = $(this).scrollTop();
if (y > 400) {
$('.Index-nav-item.active, .Index-nav-text').fadeOut().removeClass('.active');
} else if (y > 450) {
$('.Index-nav-text:nth-of-type(2)').addClass('.active').fadeIn();
} else {
$('.Index-nav-item.active, .Index-nav-text').fadeIn();
}
});
});
</script>
I'm pretty new to JS so I'm not sure if I'm going about it the best way, I can't target the next nav item and give it the class necessary at the specified height. If there's a better solution I'm all ears. Thanks for your help!