Answer the question
In order to leave comments, you need to log in
How can I make the toggleClass dependent on page scrolling affect all classes, and not just the first one?
Hello, I have a toggle which depends on page scroll:
const link = document.querySelector('.header__link');
window.addEventListener('scroll', function(){
link.classList.toggle('cactive', pageYOffset > 0);
});
Answer the question
In order to leave comments, you need to log in
querySelector()
returns only one, the first element found. And everything is necessary. This makes querySelectorAll()
const links = document.querySelectorAll('.header__link');
window.addEventListener('scroll', function(){
links.forEach((el) => el.classList.toggle('cactive', pageYOffset > 0));
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question