Hey i am trying to add a class depending on the scroll position. It worked in Jquery but i want to move to vanilla, it doesn't work. What am i missing?
IF the User scrolls to position 30px it should add the class .c-logo--scrolled
Console Error:
Uncaught TypeError: Cannot read property 'add' of undefined at add_class_on_scroll (app.min.js:17) at app.min.js:29
var scrollPosition = window.scrollY;
var logoContainer = document.getElementsByClassName('js-logo');
window.addEventListener('scroll', function() {
scrollPosition = window.scrollY;
if (scrollPosition >= 30) {
logoContainer.classList.add('c-logo--scrolled');
} else {
logoContainer.classList.remove('c-logo--scrolled');
}
});
var logoContainer = document.getElementsByClassName('js-logo');
var logoContainer = document.getElementsByClassName('js-logo')[0];
Then:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With