这里有一个误区:
.off("scroll")
确实不是表示当滚动结束时必须调用回调。
取而代之的是detach来自scroll
事件的回调。
你想要的是在滚动开始时调用回调,当它停止时调用另一个回调。
有很多方法可以做到这一点。
这是我的做法(代码是自我解释):
$(document).on('scroll',scrollStart);
// A scroll has started
function scrollStart()
{
var lastScrollY;
// record the position
lastScrollY = $(window).scrollTop();
// hide the menu
hideMenu();
// detach the event. We don't want the callback called again for now.
$(document).off('scroll',scrollStart);
// let the function scrollRunning be called after 1 sec
setTimeout(function() { scrollRunning(lastScrollY) }, 1000);
}
// A scroll is going on. Or maybe has ended
function scrollRunning(lastScrollY)
{
var currentScrollY;
// current scroll position
currentScrollY = $(window).scrollTop();
// did the position change ? Scroll is going on
// schedule scrollRunning to check again after 1 sec
if(lastScrollY != currentScrollY) // Scrolling is going on
{
lastScrollY = currentScrollY;
setTimeout(function() { scrollRunning(lastScrollY) }, 1000);
}
else // Scrolling has stopped. Show the menu and reattach the event
{
$(document).on('scroll',scrollStart);
showMenu();
}
}
你尝试读取jQuery的API文档? – MysterX
['jQuery.off'](http://api.jquery.com/off#entry-longdesc)用于删除一个处理程序,以便它不再为该事件调用。 –
'on'addEventListener,'off'删除它 – sglazkov