Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

javascript - How to detect end of scrolling

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;
});?

and some fiddle: http://jsfiddle.net/yhnKR/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

is this what you're trying to achieve:

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

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

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...