当.scroll我停止时如何进行操作

时间:2011-06-01 07:02:50

标签: javascript jquery

我已将滚动处理程序添加到我的页面中:

$(window).scroll(function() {
  ...
});

当我停止滚动栏时,我想做一些操作,而不是在我滚动它时。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

定时器通常是一个严重的黑客,但在这种情况下我想不出更好的东西。也许这样的事情对你有用:

var DELAY = 250;  // Or whatever makes sense for you.
var timer = null;

function scrolling_has_stopped() {
    timer = null;  // Make sure everything is always in a sane state.

    // Do whatever you want to do when they've stopped scrolling.
    // ...
}

$(window).scroll(function() {
    if(timer)
        clearTimeout(timer);
    timer = setTimeout(scrolling_has_stopped, DELAY);
});

基本策略是使用定时器延迟“他们已经停止滚动”动作,直到最后一次滚动事件后的几个时钟滴答,其余的只是簿记。你会想要使用DELAY值来获得感觉正确的东西。

有用的参考资料: