How to know with JavaScript which address is scrolled?

2

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.

    
asked by José Gregorio Materán 05.09.2016 в 23:03
source

1 answer

4

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 .

    
answered by 05.09.2016 / 23:55
source