I am new to this platform and would really appreciate some help with a specific issue I am facing. Despite going through numerous posts, I have not been able to find a solution yet.
Currently, I am utilizing the navbar component from Bootstrap (v5.0). My objective is to alter the color of the navbar and its links when the user scrolls down.
Although I have been able to change the color of the navbar successfully, I am unable to do the same for the nav-links. I have included a code snippet below for reference.
I would be grateful for any assistance you can provide. Thanks in advance :-)
var navBar = document.getElementById('navbar')
var textNavBars = document.querySelectorAll('a.nav-link');
window.onscroll = function () {
if (document.body.scrollTop >= 200 || document.documentElement.scrollTop >= 200) {
navBar.classList.add('nav-colored');
navBar.classList.remove('nav-transparent');
for (const textNavBar of textNavBars) {
textNavBar.classList.add('text-transparent')
textNavBar.classList.add('text-colored')
}
} else {
navBar.classList.add('nav-tranparent');
navBar.classList.remove('nav-colored');
for (const textNavBar of textNavBars) {
textNavBar.classList.add('text-colored')
textNavBar.classList.add('text-transparent')
}
}
}
.navbar{
text-transform: uppercase;
}
.nav-colored {
background-color:#FF7F2F;
transition: background-color 3s
}
.nav-transparent{
background-color:transparent;
transition: background-color 3s;
}
.text-transparent{
color: black;
}
.text-colored{
color: #FF7F2F;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spike - NavBar fotter and overall design</title>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedcd1d1cacdcaccdfcefe8b908e908c">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav class="navbar fixed-top navbar-expand-lg navbar-light" id="navbar">
<div class="container-fluid">
<a class="navbar-brand " href="#"> TEST</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle .text-colored" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown link
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item " href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<p style="padding-top: 5rem;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut deleniti odit error ...