PROJET AUTOBLOG


Shaarli - le hollandais volant

Site original : Shaarli - le hollandais volant

⇐ retour index

[JavaScript] detect down/up scrolling

mercredi 24 février 2016 à 19:26
Why didn’t I found anything simple for this ?
Here you go, without jQuery or timing-intervals :

// Initial state
var scrollPos = 0;
// adding scroll event
window.addEventListener('scroll', function(){ scrolling() });

// the function : compares the "new" scrolling state with the previous
// (this allows detecting either "up" or "down" scrolling)
// then saves the new in the $previous for the next iteration.

function scrolling() {
	if ((document.body.getBoundingClientRect()).top > scrollPos) {
		console.log('scrolling DOWN');
	} else {
		console.log('scrolling UP');
	}
	scrollPos = (document.body.getBoundingClientRect()).top;
}


Demo : http://lehollandaisvolant.net/tout/examples/js-scroll-detect.html