Scroll Direction Detection

Ever wanted to know if the user is scrolling up or down?

A simple script that will add the classes scrolling-up or scrolling-down when the user scrolls


(function() {
    var _yPos = window.pageYOffset;
    var html = document.documentElement.classList;
    window.addEventListener("scroll", function(){
        var yPos = window.pageYOffset;                
        html.add("scrolling");
        if(yPos < _yPos ){ 
            html.add("scrolling-up");
            html.remove("scrolling-down");
        }else{
            html.add("scrolling-down");
            html.remove("scrolling-up");
        }
        _yPos = yPos;
    })
    function _scroll(a, b) {
        return window.addEventListener("scroll", function() {
            clearTimeout(b);
            b = setTimeout(a, 200);
        }), a;
    }
    _scroll(function(){
        html.remove("scrolling");
    })
})();