2

我怎么能做这样的事情?

myfunction () {

    if (notscrolling) {

        // do stuff

    }
}

这是我能找到的最佳解决方案:

它获取当前滚动位置,然后在 5 毫秒后再次获取。如果数字相同,则页面不滚动!不需要全局变量。

myfunction () {

    var a = $(document).scrollTop();

    setTimeout(function() { 
        var b = $(document).scrollTop();

            if (a === b) {

                // do stuff here cuz its not scrolling :) !!!!

            }
    }, 5);

}
4

3 回答 3

1

查看http://james.padolsey.com/javascript/special-scroll-events-for-jquery/ 了解 scrollstart 和 scrollstop 事件。

使用这些事件来设置一个全局变量scrolling并检查它myfunction

于 2012-12-13T20:18:19.963 回答
1

我认为这篇文章非常符合您的要求: 如何在滚动停止时触发 ajax 请求?

您想要实现的是“scrollStop”行中的内容。

于 2012-12-13T20:19:35.883 回答
0

在窗口滚动事件中,将全局滚动变量设置为 true 或 false,然后在滚动事件中设置一个 setTimeout,在 x 毫秒后将其设置回默认值。然后,您可以使用该全局变量的值来确定窗口是否正在滚动。

window.scrolling = false;
var timer;

$(window).on('scroll',function(){
    window.scrolling = true;
    clearTimeout(timer);
    timer = setTimeout(function(){
        window.scrolling = false;
        $(window).trigger("scrollend");
    },100);
});

function myfunction() {
    if (!window.scrolling) {
        alert("Window is not scrolling.");
    }
}

甚至更好,不要使用全局范围。

(function(){
    var timer, scrolling = false;

    $(window).on('scroll',function(){
        scrolling = true;
        clearTimeout(timer);
        timer = setTimeout(function(){
            scrolling = false;
            $(window).trigger("scrollend");
        },100);
    });

    function myfunction() {
        if (!scrolling) {
            alert("Window is not scrolling.");
        }
    }
})();
于 2012-12-13T20:31:37.490 回答