Scroll Detection on a page by adding and removing classes

0

Well, I'm trying to get the header of a page, receive or lose a class, depending on the position of the page, could someone tell me what's wrong, and help me fix it?

window.onscroll = function() {

    var mobileTransp = document.getElementById('mobile-header');

    if (document.body.scrollTop > 10 || document.documentElement.scrollTop > 10) {
        mobileTransp.classList.remove('on-top');
    }
    else{
        mobileTransp.classList.add('on-top');
    }

};
    
asked by anonymous 11.10.2016 / 15:18

1 answer

0

To solve the problem, one solution was to add the event to a separate function

window.onscroll = function() {scroll()};

function scroll(){

    var mobileTransp = document.getElementById('mobile-header');

    if (document.body.scrollTop > 10 || document.documentElement.scrollTop > 10) {
        mobileTransp.classList.remove('on-top');
    }
    else{
        mobileTransp.classList.add('on-top');
    }
};
    
11.10.2016 / 15:23