我试图让菜单项获得“活动”类不仅当他们被点击时,而且滚动到ID的ID的#main ,#features等。现在我有一些jquery添加了一个活动类,并在点击时将其从其他菜单项中移除,但只要滚动显然该菜单项处于活动状态,因为它只能在点击和不滚动点上工作。将活动类添加到滚动点或ID以及点击上的菜单
<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>
我买的主题是使用stellar.js,我觉得这是创建滚动点JS:
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;
}
}
});
我只需要这一个活跃的类添加到我的菜单,并从中删除其他项目,但也滚动点或目标ID。任何帮助,将不胜感激请:)
更新后的代码,我有玩,不能正常工作:
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;
}
}
});
我想你的剧本完全一样,它没有做什么?它没有添加类活动的李或标签 – dreamsynk 2014-10-30 08:51:12
ohhh对不起,我误解了你的问题。我会尽快更新我的答案 – Kolby 2014-10-30 19:39:50