15

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/

Lukas
  • 7,384
  • 20
  • 72
  • 127
  • possible duplicate of [How to check if a user has scrolled to the bottom](http://stackoverflow.com/questions/3898130/how-to-check-if-a-user-has-scrolled-to-the-bottom) – Rafay Nov 10 '12 at 10:06
  • 3
    no dude, i don't want to alert at the bottom of my site, but at the end of scrolling action, this is huge different, but thx – Lukas Nov 10 '12 at 10:28
  • possible duplicate of [jQuery: Event, when User stops scrolling](http://stackoverflow.com/questions/3701311/jquery-event-when-user-stops-scrolling) – Stephan Muller Sep 22 '14 at 21:56

2 Answers2

17

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/

Andy
  • 29,707
  • 9
  • 41
  • 58
  • no, i need to detect stop of my scrolling action, i'm on the page, i'v started of scrolling and after scroll some space i'v stoped, so i need to detect that moment - when i'm ending it – Lukas Nov 10 '12 at 10:24
  • @ŁukaszBorawski it wasn't clear what exactly you're trying to do from your question and code sample. But i have updated my answer. – Andy Nov 10 '12 at 12:09
2

maybe adding new events like this:

http://james.padolsey.com/javascript/special-scroll-events-for-jquery/

would help

Nikes
  • 1,094
  • 8
  • 13