2014-10-30 69 views
0

我试图让菜单项获得“活动”类不仅当他们被点击时,而且滚动到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; 
    } 
} 
}); 

回答

0
win.scroll(function(e){ 
    jQuery.each(items, function(key, value){ 
     var item = jQuery(value); 
     if(win.scrollTop() >= item.offset().top){ 
      jQuery('.menu-item a.active').removeClass('active'); 
      var id = item.attr('id'); 

      jQuery.each(navItems, function(key, value){ 
       var navItem = jQuery(value); 
       if(navItem.attr('href') == '#'+id) navItem.addClass('active'); 
      }); 
     } 
    }); 
}); 

对不起,我误解你的问题。我认为这是你在找什么: http://jsfiddle.net/pnsxg2zh/

所以基本上上滚动我得到窗口的滚动位置,然后用offset().top找到你想要滚动到元素的位置。我检查窗口的滚动位置是否是> =元素的顶部,如果是,我将活动类添加到具有等于该元素的id的导航项目。

+0

我想你的剧本完全一样,它没有做什么?它没有添加类活动的李或标签 – dreamsynk 2014-10-30 08:51:12

+0

ohhh对不起,我误解了你的问题。我会尽快更新我的答案 – Kolby 2014-10-30 19:39:50

0

因此,当单击菜单项并且元素(#main和其他)在屏幕上可见时,您需要添加“活动”类。

添加“积极”级上的点击,这样做:

jQuery('.menu-item').click(function(){ 
    jQuery('#menu-main-menu li').removeClass('active'); 
    jQuery(this).removeClass('menu-item').addClass('menu-item active'); 
}); 

检查元素的可见性,请阅读这篇文章

Check if element is visible after scrolling

+0

嘿,点击部分我已经完全像你在那里,它在屏幕上可见的部分,我看看你给我的链接,我的js是可怕的,我无法弄清楚那里发生了什么? – dreamsynk 2014-10-30 09:27:36

+0

我更新了一段代码,我会想象应该工作,但我做错了什么,它基本上是使用当前的主题代码添加类活动目标,然后我试图让它来检查该目标是否有类活动增加它的菜单项,可能会更好地通过匹配的ID与菜单项中的哈希做到这一点? – dreamsynk 2014-10-30 09:40:17

+0

所以你想添加“.active”来标记一个里面的li? – 2014-10-30 16:13:27

相关问题