I am trying to highlight the menu item when you scroll down to the section. The highlighting works but for some reason I can't remove the highlighting when scrolled to an other section
This is what my menu looks like:
<div id="navbar">
<ul class="nav navbar-nav">
<li class="active"><a data-id="home" href="#home">Home</a></li>
<li class="navbar-right"><a data-id="cont" href="#contact">Contact</a></li>
<li class="navbar-right"><a data-id="exp" href="#exp">Expertise</a></li>
<li class="navbar-right"><a data-id="wie2" href="#wie2">Wie</a></li>
</ul>
</div>
In the html for every section where I use the id anchor I added class="section"
This is my jQuery:
jQuery(window).scroll(function () {
var position = jQuery(this).scrollTop();
jQuery('.section').each(function() {
var target = jQuery(this).offset().top;
var id = jQuery(this).attr('id');
if (position >= target) {
jQuery('#navbar>ul>li>a').removeClass('clicked');
jQuery('#navbar ul li a[data-id=' + id + ']').addClass('clicked');
}
});
});
Anyone has any idea why the class get deleted everytime? becuase when I comment out jQuery('#navbar>ul>li>a').removeClass('clicked');
it works great. The classes are being added correctly. But removing them doesn't work :(