classList is only affect the first li in the ul despect the fact that all the elements where selected.
document.addEventListener("scroll", function() {
const headerChangejh = document.querySelector(".nav-link");
if (window.scrollY > 0) {
headerChangejh.classList.add("scrolledy");
} else {
headerChangejh.classList.remove("scrolledy");
}
})
.nav-link {
font-size: 1.2rem;
font-weight: 800;
transition: 0.7s ease;
color: black;
}
.nav-link:hover {
color: red;
.nav-link.scrolledy {
color: #2196f3;
}
<ul class="nav-menu">
<li class="nav-item">
<a href="#" class="nav-link">For Individual</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">For Companies</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">App</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Jobs</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Contact us</a>
</li>
</ul>
the above code only changes the first li in the ul, while other remain unchange despite the fact that all the elements was selected
I'm unsure what effect you're after, as the current
'scroll'
event-listener simply colors all.nav-link
elements based on whether or not the window has scrolled - instead of coloring elements based on, say, how far down the page they are or how much the user scrolled-passed, around, or over them. Your question-post does not describe the ultimate effect you're after, so I largely left it as-is.As for bugs-in-your-code:
You need to use
querySelectorAll
, notquerySelector
, to get all matching elements - usingquerySelector
will return only the first matching element (i.e. your "For Individual"<li>
)querySelectorAll
, yourscroll
event-listener does not iterate over all matched elements: it just has a singleif
to add/remove a CSS class-name. I've replaced it with afor(of)
loop (which is better than using.forEach
because there's no implicit closure) that uses thetoggle( className, force )
method, for more succinct code.Your CSS was malformed: you needed to fix it, as below:
Malformed CSS:
Fixed CSS:
Additionally, I made the
.nav-link
elements bigger so you can better see thetransition:
to theircolor
property.