I've been working on changing the navigation link color when scrolling in Bootstrap 5. To illustrate the problem, I've created a fiddle which you can check out here:
https://jsfiddle.net/mfen723/8kvf0431/20/
My approach involves using the following function to update the color of the nav links as the user scrolls:
const navUlScroll = document.querySelector('a.nav-link');
window.addEventListener('scroll', () => {
if (window.scrollY >= 26) {
navUlScroll.classList.add('ul.navbar-nav-scroll');
} else if (window.scrollY < 56)
navUlScroll.classList.remove('ul.navbar-nav-scroll');
});
After examining the nav links in the dev tools post-scrolling, I noticed that the ul.navbar-nav-scroll
class is indeed added to the links but it doesn't affect the color. The CSS for this class is as follows:
ul.navbar-nav-scroll .nav-link {
color: var(--blue-accent-light) !important;
}
Even with the use of the !important rule to override previous styles, the desired outcome is not achieved. Any assistance in solving this issue would be greatly appreciated.