如何检测滚动结束 [英] How to detect end of scrolling

查看:131
本文介绍了如何检测滚动结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本有些问题。所以,我想检测滚动操作的结束。当我滚动时,我有警报,但如果我结束它,则不会。你能帮助我吗?这是我的代码:

i have some problems with my script. So, i want to detect end of my scrolling action. I have my alert when i'm scrolling but not if i'm ending it. Can you help me? This is my code:

var animatable = $('body, html');
var animating = false;
animatable.animate( {scrollTop: $('#foo').offset()})

$(window).scroll(function(e) { 
       if(!animating){
           animatable.stop(false, true); 
           alert('stop scrolling');
       }
       animating = false;
});​

和一些小提琴: http://jsfiddle.net/yhnKR/

推荐答案

这就是你想要实现的目标:

is this what you're trying to achieve:

$('body').animate( {scrollTop: $('#foo').offset().top},1000,function(){
 alert('stop scrolling');   
});

http://jsfiddle.net/yhnKR/2/

如果您使用滚动设置动画,则无需观看滚动事件jquery。

You don't have to watch the scroll event if you animate the scroll with jquery.

好的,如果你想检测用户何时停止滚动,你将不得不使用超时来检查用户是否已停止。否则,您将获得每个滚动步骤的事件。
喜欢这个:

Ok, if you want to detect when the user stopped scrolling, you'll have to use a timeout to check if the user stopped. Otherwise you'll get the event for each scroll step. Like this:

var delay = 1000;
var timeout = null;
$(window).bind('scroll',function(){
    clearTimeout(timeout);
    timeout = setTimeout(function(){
        alert('scrolling stopped');
    },delay);
});​​​​​​​​​​

http://jsfiddle.net/yhnKR/4/

这篇关于如何检测滚动结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆