JavaScript addEventListener

by hsablonniere

HTML

<div id="dir"></div>

CSS

body {
  height: 1000px;
}

#dir {
  font-size: 300px;
  text-align: center;
  position: fixed;
  -moz-transition: all 300ms ease-in;
  -ms-transition: all 300ms ease-in;
  -o-transition: all 300ms ease-in;
  -webkit-transition: all 300ms ease-in;
  transition: all 300ms ease-in;
  width: 100%;
}

.up {
  background-color: #AAF;
}

.down {
  background-color: #FFA;
}

JavaScript

(function() {
    var lastScroll = 0;
    var dir = document.getElementById('dir');

    window.addEventListener('scroll', function(ev) {
        if (window.scrollY > lastScroll) {
            document.body.className = 'down';
            dir.innerHTML = '&#8595;';
        } else {
            document.body.className = 'up';
            dir.innerHTML = '&#8593;';
        }
        lastScroll = window.scrollY;
    }, false);
})();