0

我正在处理这篇文章中的建议Change Active Menu Item on Page Scroll? 但似乎无法让它工作。

这是我的页面:http ://terminalcitymarketing.com/drafts/liftx/flexslider-2/about-us

这是HTML:

<nav class="side-nav">                                          


    <ul class="menu">
        <li class="page_item"><span style="padding-left:10px;"><strong>Jump To Section</strong></span></li>
        <li class="page_item current_page_item"><a href="#lift-x"><span>Lift-X</span></a></li>
        <li class="page_item"><a href="#team"><span>Our Team</span></a></li>
        <li class="page_item"><a href="#community"><span>Lift-X in the Community</span></a></li>
        <li class="page_item"><a href="#join"><span>Join Our Team</span></a></li>   
    </ul>   

</nav>

这是脚本:

// Cache selectors
var lastId,
    topMenu = $j(".side-nav"),
    topMenuHeight = topMenu.outerHeight()+15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $j($j(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 = $j(this).attr("href"),
      offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
  $j('html, body').stop().animate({
      scrollTop: offsetTop
  }, 300);
  e.preventDefault();
});

// Bind to scroll
$j(window).scroll(function(){
   // Get container scroll position
   var fromTop = $j(this).scrollTop()+topMenuHeight;

   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($j(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("current_page_item")
         .end().filter("[href=#"+id+"]").parent().addClass("current_page_item");
   }
});

有任何想法吗?

4

0 回答 0