首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滚动时活动

滚动时活动
EN

Stack Overflow用户
提问于 2012-06-17 02:25:50
回答 1查看 613关注 0票数 0

我试图在scroll上突出显示一个不同的列表项,但我现在无法让它工作,因为我将它移植到了这个wordpress安装程序中。

这是我目前的菜单:

代码语言:javascript
复制
<div id="navigation">
    <ul>
        <li class="nav1">
            <a href="#post-31"></a>
        </li>
        <li class="nav2">
            <a href="#post-28"></a>
        </li>
        <li class="nav3">
            <a href="#post-17"></a>
        </li>
        <li class="nav4">
            <a href="#post-12"></a>
        </li>
        <li class="nav5">
            <a href="#post-5"></a>
        </li>
        <li class="nav6">
            <a href="#post-1"></a>
        </li>
    </ul>
</div>

这是Javascript:

代码语言:javascript
复制
//#### Change Active Menu Item 
// Cache selectors
var lastId,
    topMenu = $("#navigation ul"),
    topMenuHeight = topMenu.outerHeight()+15,
    // 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 }, 300);
  e.preventDefault();
});

// Bind to scroll
$(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");
   }                   
});

我可以断定脚本在某种程度上是有效的,因为当滚动它将活动类放在#post-1列表项上时,它不会从那里移动。

在代码中可以看到任何不正确的地方吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-18 07:01:39

出于某种原因,在菜单中添加“order”、“=>”和“ASC”似乎能让它正常工作。我不知道为什么,但它现在起作用了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11065986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档