Javascript 滚动激活

Javascript 滚动激活,javascript,jquery,wordpress,html,wordpress-theming,Javascript,Jquery,Wordpress,Html,Wordpress Theming,我试图在scroll上突出显示一个不同的列表项,但由于我将其移植到wordpress安装中,所以无法使其正常工作 这是我当前的菜单: <div id="navigation"> <ul> <li class="nav1"> <a href="#post-31"></a> </li> <li class="nav2">

我试图在scroll上突出显示一个不同的列表项,但由于我将其移植到wordpress安装中,所以无法使其正常工作

这是我当前的菜单:

<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:

//#### 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");
   }                   
});
/#####更改活动菜单项
//缓存选择器
var lastId,
topMenu=$(“#导航ul”),
topMenuHeight=topMenu.outerHeight()+15,
//所有列表项
menuItems=topMenu.find(“a”),
//与菜单项相对应的锚定
scrollItems=menuItems.map(函数(){
var item=$($(this.attr(“href”));
if(item.length){return item;}
});
//将单击处理程序绑定到菜单项
//所以我们可以得到一个奇特的卷轴动画
菜单项。单击(函数(e){
var href=$(this.attr(“href”),
offsetTop=href==“#”?0:$(href).offset()。顶部菜单高度+1;
//$('html,body').stop().animate({scrollTop:offsetTop},300);
e、 预防默认值();
});
//绑定到滚动
$(窗口)。滚动(函数(){
//获取容器滚动位置
var fromTop=$(this.scrollTop()+topMenuHeight;
//获取当前滚动项目的id
var cur=scrollItems.map(函数(){
if($(this).offset().top
我可以判断脚本是否正常工作,因为当滚动时,它会将活动类放在#post-1列表项上,但它不会从那里移动


代码中有什么不正确的地方吗?

出于某种原因,在菜单中添加'order'=>'ASC'似乎可以让它工作。我不知道为什么,但它现在起作用了

你有没有可能把它放到一个JSFIDLE中,这样我就可以玩了around@Alex可能不是,我遇到的问题是,它在静态站点上运行良好,但当我将其添加到wordpress主题时,问题就开始出现了。。我将自定义JS文件中的所有“$”转换为“jQuery”,但仍然无法正常工作。。有没有办法检查javascript是否与wordpress中的任何内容冲突?@joebby,一个好办法是,如果确实可以将其发布到JSFIDLE上。从真实的htm输出开始,然后添加js。如果它起作用,那么问题可能是一些碰撞。如果没有,那么问题就出在你的js中,与WP的其余部分无关。静态html代码是否与WP的输出完全相同?