Hi everyone, this is my first time posting a question here so please be gentle :)
I recently implemented a script to change the CSS of my navbar when scrolling.
window.addEventListener("scroll", function () {
let header = document.querySelector(".navbar");
let windowPosition = window.scrollY > 0;
header.classList.toggle("change-bg-nav", windowPosition);});
Here is the CSS for the "change-bg-nav" class:
.change-bg-nav {
box-shadow: var(--box-shadow);
background-color: rgb(255, 255, 255, 0.9) !important;
backdrop-filter: blur(5.5px);}
The scroll effect works perfectly on all device sizes but now I am trying to achieve the same effect when clicking on the hamburger icon to expand the menu without having scrolled. I attempted using media queries but it didn't give me the desired result. Any suggestions on how to approach this?
Below is the code for my navigation menu:
<nav class="navbar navbar-expand-lg navbar-light bg-light px-3 mb-5">
<div class="container-fluid">
<a class="navbar-brand" href="/">Brand name</a>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup"
aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ms-auto">
<a class="nav-link active" aria-current="page" href="/">Home</a>
<a class="nav-link" href="/about.html">About</a>
<a class="nav-link" href="/about.html#contact-me">Contact</a>
</div>
<div class="navbar-nav social-media">
<a href="#" target="_blank" class="nav-link social"><i class="fab fa-twitter px-1"></i></a>
<a href="#" target="_blank" class="nav-link social"><i class="fab fa-linkedin-in px-1"></i></a>
</div>
</div>
</div>
</nav>