您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

用户停止滚动时的事件

用户停止滚动时的事件

您可以使scroll()每次用户滚动时都有一个超时被覆盖。这样,当他在一定毫秒后停止运行时,您的脚本将运行,但是如果他在此期间滚动,则计数器将重新开始,并且脚本将等待直到完成滚动为止。

因为这个问题又采取了一些行动,所以我认为我不妨使用添加scrollEnd事件的jQuery扩展对其进行更新。

// extension:

$.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));

  });

};



// how to call it (with a 1000ms timeout):

$(window).scrollEnd(function(){

    alert('stopped scrolling');

}, 1000);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



<div style="height: 200vh">

  Long div

</div>
其他 2022/1/1 18:17:38 有493人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶