Event when user stops scrolling

2019-01-02 17:45发布

I'd like to do some fancy jQuery stuff when the user scrolls the page. But I have no idea how to tackle this problem, since there is only the scroll() method.

Any ideas?

8条回答
荒废的爱情
2楼-- · 2019-01-02 18:09

Here is another more generic solution based on the same ideas mentioned:

var delayedExec = function(after, fn) {
    var timer;
    return function() {
        timer && clearTimeout(timer);
        timer = setTimeout(fn, after);
    };
};

var scrollStopper = delayedExec(500, function() {
    console.log('stopped it');
});

document.getElementById('box').addEventListener('scroll', scrollStopper);
查看更多
姐姐魅力值爆表
3楼-- · 2019-01-02 18:12

There are scrollstart and scrollstop functions that are part of jquery mobile.

Example using scrollstop:

$(document).on("scrollstop",function(){
   alert("Stopped scrolling!");
});

Hope this helps someone.

查看更多
素衣白纱
4楼-- · 2019-01-02 18:16

Why so complicated? As the documentation points out, this http://jsfiddle.net/x3s7F/9/ works!

$('.frame').scroll(function() {
 $('.back').hide().fadeIn(100);
}

http://api.jquery.com/scroll/.


Note: The scroll event on Windows Chrome is differently to all others. You need to scroll fast to get the same as result as in e.g. FF. Look at https://liebdich.biz/back.min.js the "X" function.

Some findings from my how many ms a scroll event test:

  • Safari, Mac FF, Mac Chrome: ~16ms an event.
  • Windows FF: ~19ms an event.
  • Windows Chrome: up to ~130ms an event, when scrolling slow.
  • Internet Explorer: up to ~110ms an event.

http://jsfiddle.net/TRNCFRMCN/1Lygop32/4/.

查看更多
残风、尘缘若梦
5楼-- · 2019-01-02 18:16

I had the need to implement onScrollEnd event discussed hear as well. The idea of using timer works for me.

I implement this using JavaScript Module Pattern:

var WindowCustomEventsModule = (function(){

    var _scrollEndTimeout = 30;

    var _delayedExec = function(callback){
        var timer;
        return function(){
            timer && clearTimeout(timer);
            timer = setTimeout(callback, _scrollEndTimeout);
        }
    };

    var onScrollEnd = function(callback) { 
        window.addEventListener('scroll', _delayedExec(callback), false);         
    };

    return {
        onScrollEnd: onScrollEnd
    }
})();

// usage example
WindowCustomEventsModule.onScrollEnd(function(){
    //
    // do stuff
    //
});

Hope this will help / inspire someone

查看更多
栀子花@的思念
6楼-- · 2019-01-02 18:23

I pulled some code out of a quick piece I cobbled together that does this as an example (note that scroll.chain is an object containing two arrays start and end that are containers for the callback functions). Also note that I am using jQuery and underscore here.

$('body').on('scroll', scrollCall);
scrollBind('end', callbackFunction);
scrollBind('start', callbackFunction);

var scrollCall = function(e) {
    if (scroll.last === false || (Date.now() - scroll.last) <= 500) {
        scroll.last = Date.now();
        if (scroll.timeout !== false) {
            window.clearTimeout(scroll.timeout);
        } else {
            _(scroll.chain.start).each(function(f){
                f.call(window, {type: 'start'}, e.event);
            });
        }
        scroll.timeout = window.setTimeout(self.scrollCall, 550, {callback: true, event: e});
        return;
    }
    if (e.callback !== undefined) {
        _(scroll.chain.end).each(function(f){
            f.call(window, {type: 'end'}, e.event);
        });
        scroll.last = false;
        scroll.timeout = false;
    }
};

var scrollBind = function(type, func) {
    type = type.toLowerCase();
    if (_(scroll.chain).has(type)) {
        if (_(scroll.chain[type]).indexOf(func) === -1) {
            scroll.chain[type].push(func);
            return true;
        }
        return false;
    }
    return false;
}
查看更多
浅入江南
7楼-- · 2019-01-02 18:24

There is no such event as 'scrollEnd'. I recommend that you check the value returned by scroll() every once in a while (say, 200ms) using setInterval, and record the delta between the current and the previous value. If the delta becomes zero, you can use it as your event.

查看更多
登录 后发表回答