How was the address scroll on the page? I want that when you scroll down, you do an action and another action when you scroll up.
How was the address scroll on the page? I want that when you scroll down, you do an action and another action when you scroll up.
You can do this by storing the previous value of scrollTop and comparing it with its current value, all this with the scroll event of the document:
var lastScrollTop = 0;
window.addEventListener("scroll", function(){
var st = window.pageYOffset || document.documentElement.scrollTop;
if (st > lastScrollTop){
console.log('para abajo');
} else {
console.log('para arriba');
}
lastScrollTop = st;
}, false);
.test {
height: 1500px;
width: 500px;
background-color: blue;
}
<div class="test">
</div>
This response was based on a existing in SO in English .