滚动间谍效果工作正常,但突然停止工作。我的意思是,当 href 链接像这样 href="#id" 那样给出时,滚动单页站点时会识别菜单的活动元素。但当给出像“/home#id”这样的 href 时,它就会停止工作。我想像这样使用它。由于该网站使用通用导航。
这是我的代码:
/ Cache selectors
var lastId,
topMenu = $("#timenav"),
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");
}
});
有人可以帮我弄这个吗
http://www.drkeenly.com/ http://www.drkeenly.com/
这是一个小提琴
http://jsfiddle.net/aGjTV/ http://jsfiddle.net/aGjTV/
任何帮助都会很棒。提前致谢 !
var cur = scrollItems!=null?scrollItems.map(...):null;
在此行之后,cur 要么为某值,要么为 null。
如果它是空的,那么你不能像数组一样索引它,或者用 cur.length 获取它的长度。
您还没有指定错误,但我敢打赌它会说“TypeError:cur is null”。
解决方案取决于您实际希望代码执行的操作,但我建议将函数的其余部分包装在
if (cur) {
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)