views:

24

answers:

1

How can I trigger a function when the browser window stops scrolling? Either by mouse wheel, click, space bar or arrow key? Is there any event for such action? I have tried to search online but couldn't get any solution. I'm fine with a jQuery solution.

+4  A: 

There's no "event" but you could make your own, like this:

$(function() {
  var timer;
  $(window).scroll(function() {
    clearTimeout(timer);
    timer = setTimeout(function() {
      $(window).trigger("srollStop");
    }, 250);
  });
});

Then you could bind to it, like this:

$(window).bind("scrollStop", function() {
  alert("No one has scrolled me in 250ms, where's the love?");
});

This creates an event, there's no "stop" for this, but you can define your own...in this case "stop" is defined as "hasn't scrolled in 250ms", you can adjust the timer to your liking, but that's the idea.

Also if you're just doing one thing there's no need for an event, just put your code where I'm calling $(window).trigger("srollStop"); and it'll run nn milliseconds after the scroll stops.

Nick Craver
Damn, beat me by 15 seconds ;)
Tatu Ulmanen
yeah! where's the love? +1
Reigel