I am trying to get the menu items to get the "active" class not only when they are clicked on but also when scrolled to the id's of #main, #features etc. Right now I have some jquery that adds an active class and removed it from other menu items on click, but as soon as a scroll obviously that menu item stats active, as its only working on clicks and not scroll points.
<ul id="menu-main-menu" class="nav navbar-nav">
<li class="menu-item"><a title="Home" href="#main">Home</a></li>
<li class="menu-item"><a title="Features" href="#features">Features</a></li>
<li class="menu-item"><a title="About" href="#about">About</a></li>
<li class="menu-item"><a title="Contact us" href="#fill">Contact us</a></li>
</ul>
The theme i bought is using stellar.js and I think this is the js creating scroll points:
jQuery('a[href*=#]:not([href=#])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') || location.hostname == this.hostname) {
var target = jQuery(this.hash);
target = target.length ? target : jQuery('[name=' + this.hash.slice(1) + ']');
if (target.length) {
jQuery('html,body').animate({
scrollTop: target.offset().top - 100
}, 1000);
return false;
}
}
});
I just need this to add an active class to my menu and remove it from other items but also on scroll point or target id. Any help would be much appreciated please :)
Updated code I am playing with, not working properly:
jQuery('a[href*=#]:not([href=#])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') || location.hostname == this.hostname) {
var target = jQuery(this.hash);
target = target.length ? target : jQuery('[name=' + this.hash.slice(1) + ']');
if (target.length) {
jQuery('html,body').animate({
scrollTop: target.offset().top - 100
}, 1000);
jQuery(".menu-item a").removeClass("active");
target.addClass("active");
target.hasClass("active").find(this).addClass("active");
return false;
}
}
});