So, I am trying to make navbar swap colors of text and background when scrolled, with the following code it seems that background part is working just fine and it switches to white and back to gray, but when I literally changed font color in the next line, it just doesn't work. All the links are grouped in a list with the id = "nav_links". What's the deal with this? Thanks in advance!
function myFunction() {
if (document.body.scrollTop >= 0) {
console.log(document.body.scrollTop);
nav.style.backgroundColor = "white";
document.getElementById('nav_links').style.color = 'rgb(25,25,25)';
if (document.body.scrollTop === 0) {
nav.style.backgroundColor = 'rgb(25,25,25)';
document.getElementById('nav_links').style.color = "white";
}
}
}
document.addEventListener("scroll", myFunction);
<header id="nav">
<img src="logo.png" alt="logo" id="logo">
<nav>
<ul id="nav_links">
<li><a href="#">Lifestyle</a></li>
<li><a href="#">Fashion</a></li>
<li><a href="#">Beauty</a></li>
<li><a href="#">Health</a></li>
</ul>
</nav>