From here
jQuery - associate event with scroll stop
$.fn.scrollStopped = function(callback) { $(this).scroll(function(){ var self = this, $this = $(self); if ($this.data('scrollTimeout')) { clearTimeout($this.data('scrollTimeout')); } $this.data('scrollTimeout', setTimeout(callback,250,self)); }); }; $(window).scrollStopped(function(){ $("div").withinViewport().append("<span>hi</span>"); });
How it works.
The function first clears any timeout associated with the scrollTimeout data scrollTimeout .
He thinks he is creating a new element there with an interval of 250 ms with the transferred function.
Thus, while the scroll moves, it always clears the function from starting and restarts it to start “a little”.
When scrolling stops, then it cannot clear the function for the function to execute.
Nice trick.
Hogan source share