4

I'm trying to call a function when the user stops to scroll. But i don't want to use "scrollstop" in jQuery mobile since I don't need jQuery mobile anywhere else. Is there an equivalent event in normal jQuery or JS?

olivers
  • 188
  • 1
  • 1
  • 10
  • 1
    There's [this question](http://stackoverflow.com/questions/4620906/how-do-i-know-when-ive-stopped-scrolling-javascript) already on a vanilla.js solution, as well as the [`jquery-scrollstop`](https://github.com/ssorallen/jquery-scrollstop) plugin. – Huey Jul 15 '15 at 10:56

1 Answers1

2

The only answer I found was still using jquery, and for some reasons was not working in my project.

So here is my version, no jQuery needed, enjoy!

Note : Here is a snippet, but since it's embedded, it will delay the event a lot

var lastTimeScrolled = null;
var theElementYouCareAbout = document;
var intervalMilliSeconds = 500; // interval goes here
theElementYouCareAbout.onscroll = function(){
  if (performance.now() - lastTimeScrolled > intervalMilliSeconds){
    var intervalScroll = setInterval(function(){
      if (performance.now() - lastTimeScrolled > intervalMilliSeconds){
        doSomething();
        clearInterval(intervalScroll);
      }
    }.bind(intervalScroll).bind(intervalMilliSeconds), 100);
  }
  lastTimeScrolled = performance.now();
}.bind(intervalMilliSeconds);

function doSomething (){
  alert('You just stopped scrolling :)');
}
<div style="height: 200vh">
  Scroll me!
</div>
John
  • 376
  • 2
  • 9