dantz dantz - 1 year ago 115
Javascript Question

Event when user stops scrolling

I'd like to do some fancy jQuery stuff when the user scrolls the page. But I have no idea how to tackle this problem, since there is only the scroll() method.

Any ideas?

Answer Source

You can make the scroll() have a time-out that gets overwritten each times the user scrolls. That way, when he stops after a certain amount of milliseconds your script is run, but if he scrolls in the meantime the counter will start over again and the script will wait until he is done scrolling again.


Because this question got some action again I figured I might as well update it with a jQuery extension that adds a scrollEnd event

// extension:
$.fn.scrollEnd = function(callback, timeout) {          
    var $this = $(this);
    if ($this.data('scrollTimeout')) {
    $this.data('scrollTimeout', setTimeout(callback,timeout));

// how to call it (with a 1000ms timeout):
    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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download