【问题标题】:Sticky navigation not adding active links correctly粘性导航未正确添加活动链接
【发布时间】:2018-09-26 06:12:46
【问题描述】:

我正在尝试实现导航。我也使用this code 来做同样的事情。

<nav>
<ul id="mainNav">
    <li class="active"><a href="#home">Home</a></li>
    <li><a href="#work">Work</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#contact">Contact</a></li>    
  </ul>
</nav>
<section id="home"><h2>Home</h2></section>
<section id="work" data-sr><h2>Work</h2></section>
<section id="about"><h2>About</h2></section>
<section id="contact"><h2>Contact</h2></section>

// Cache selectors

var lastId,
 topMenu = $("#mainNav"),
 topMenuHeight = topMenu.outerHeight()+1,
 // All list items
 menuItems = topMenu.find("a"),
 // Anchors corresponding to menu items
 scrollItems = menuItems.map(function(){
   var item = $($(this).attr("href"));
    if (item.length) { return item; }
 });

// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
  var href = $(this).attr("href"),
      offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
  $('html, body').stop().animate({ 
      scrollTop: offsetTop
  }, 850);
  e.preventDefault();
});
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop()+topMenuHeight;

   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";

   if (lastId !== id) {
       lastId = id;
       // Set/remove active class
       menuItems
         .parent().removeClass("active")
         .end().filter("[href=#"+id+"]").parent().addClass("active");
   }                   
});

要求:- 我希望导航具有可以根据要求交换的链接。

问题:- 在上面的 codepen 中,当我交换链接时,它没有正确添加活动类。如果我交换导航链接和部分链接,那么只有它才能正常工作。 例如,如果我的导航有 HOME、WORK、ABOUT 和 CONTACT。然后我应该能够将工作链接的位置与联系链接交换,并且我的粘性导航仍然应该正确添加活动类而不改变它们各自的部分。

请帮我实现上述场景。

【问题讨论】:

    标签: javascript jquery scroll navigation


    【解决方案1】:

    下面的代码应该可以解决你的问题。

    // Cache selectors
    var lastId,
     topMenu = $("#mainNav"),
     topMenuHeight = topMenu.outerHeight()+1,
     // All list items
     menuItems = topMenu.find("a"),
     // Anchors corresponding to menu items
     scrollItems = $('section');
    
    // Bind click handler to menu items
    // so we can get a fancy scroll animation
    menuItems.click(function(e){
      var href = $(this).attr("href"),
          offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
      $('html, body').stop().animate({ 
          scrollTop: offsetTop
      }, 850);
      e.preventDefault();
      setActiveNav(href);
    });
    
    function setActiveNav(href) {
      menuItems.each((index, menu) => {
        if(menu.hash === href) {
          $(menu).parent().addClass('active');
        } else {
          $(menu).parent().removeClass('active');
        }
      });
    }
    
    $(window).scroll(function(){
       // Get container scroll position
       var fromTop = $(this).scrollTop();
    
    
       // Get id of current scroll item
       var cur = scrollItems.toArray().findIndex(item => {
        return (item.offsetTop >= fromTop);
    });
       // Get the id of the current element
       var id = cur && cur > -1 ? scrollItems[cur].id : "";
    
       if (lastId !== id) {
           lastId = id;
           // Set/remove active class
           menuItems
             .parent().removeClass("active")
             .end().filter("[href=#"+id+"]").parent().addClass("active");
       }
    });
    

    将您的功能构建在单独的功能中将解决未来的维护开销。

    主要问题在于结构、元素索引和位置。

    【讨论】:

    • 嗨,你能否像在 codepen 中一样在滚动上进行这项工作?
    • @amy 这是我用过的codepen [codepen.io/anon/pen/eLqdyX。请看一下,让我知道您的具体要求是什么。
    • 是的。感谢您让它在点击时工作。但我也希望在滚动时具有相同的行为(添加活动类)。就像如果您滚动,那么特定部分将被突出显示(相同它在我发布的代码笔中的工作方式)
    • @amy 我已更新代码以适用于滚动和单击功能。请检查并让我知道所需的任何更改。
    • 2 件事 - 1。它拒绝检测索引变为 0 时的 id。其次,当索引为 -1 时,它给我一个无法读取未定义错误的属性 'id'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    相关资源
    最近更新 更多