我试图在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");
}
});我可以断定脚本在某种程度上是有效的,因为当滚动它将活动类放在#post-1列表项上时,它不会从那里移动。
在代码中可以看到任何不正确的地方吗?
发布于 2012-06-18 07:01:39
出于某种原因,在菜单中添加“order”、“=>”和“ASC”似乎能让它正常工作。我不知道为什么,但它现在起作用了
https://stackoverflow.com/questions/11065986
复制相似问题