jQuery scroll() detect when user stops scrolling

前端 未结 13 1440
情歌与酒
情歌与酒 2020-11-22 02:17

Ok with this..

$(window).scroll(function()
{
    $(\'.slides_layover\').removeClass(\'showing_layover\');
    $(\'#slides_effect\').show();
});
13条回答
  •  借酒劲吻你
    2020-11-22 03:00

    Using jQuery throttle / debounce

    jQuery debounce is a nice one for problems like this. jsFidlle

    $(window).scroll($.debounce( 250, true, function(){
        $('#scrollMsg').html('SCROLLING!');
    }));
    $(window).scroll($.debounce( 250, function(){
        $('#scrollMsg').html('DONE!');
    }));
    

    The second parameter is the "at_begin" flag. Here I've shown how to execute code both at "scroll start" and "scroll finish".

    Using Lodash

    As suggested by Barry P, jsFiddle, underscore or lodash also have a debounce, each with slightly different apis.

    $(window).scroll(_.debounce(function(){
        $('#scrollMsg').html('SCROLLING!');
    }, 150, { 'leading': true, 'trailing': false }));
    
    $(window).scroll(_.debounce(function(){
        $('#scrollMsg').html('STOPPED!');
    }, 150));
    

提交回复
热议问题