jQuery scroll结束事件和resize结束事件

通过设置timeout,判断滚动事件和resize事件的结束

scroll end事件

$.fn.scrollEnd = function(callback, timeout) {          
  $(this).scroll(function(){
    var $this = $(this);
    if ($this.data('scrollTimeout')) {
      clearTimeout($this.data('scrollTimeout'));
    }
    $this.data('scrollTimeout', setTimeout(callback,timeout));
  });
};

//with a 1000ms timeout
$(window).scrollEnd(function(){
    alert('stopped scrolling');
}, 1000);

resize end事件

$.fn.resizeEnd = function (callback, timeout) {
    $(this).resize(function () {
        var $this = $(this);
        if ($this.data('resizeTimeout')) {
            clearTimeout($this.data('resizeTimeout'));
        }
        $this.data('resizeTimeout', setTimeout(callback, timeout));
    });
};

$(document).resizeEnd(function () {
    alert('stopped resizing');
}, 800);

https://stackoverflow.com/questions/3701311/event-when-user-stops-scrolling

你可能感兴趣的:(js)