There is a JavaScript function in place for a responsive navigation system that includes a burger button to toggle the visibility of the navigation menu when the screen size is too small.
The issue I am facing is that despite setting the CSS style as display:none, the navigation links are displayed upon loading the page. However, after initial load, the burger button functions correctly and allows toggling between display: none and display: flex. What could be causing this behavior where display: none is ignored on load?
function myBurger() {
var x = document.getElementById("navLinks");
if (x.style.display === "none") {
x.style.display = "flex";
} else {
x.style.display = "none";
}
}
.navigation1 {
width: 100%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
padding: 3rem 3rem;
.logo img {
height: 5rem;
}
i {
display: none;
}
.navLinks {
display: flex;
a {
padding-left: 2rem;
align-self: center;
color: $secondaryColor;
}
}
}
@media (max-width: 700px) {
#icon {
align-self: center;
i {
font-size: 3rem;
display: block;
}
}
.navLinks {
flex-direction: column;
width: 100%;
padding-top: 2rem;
padding-bottom: 2rem;
display: none;
.nav-link {
padding-left: 0;
}
}
}
<div class="navigation1">
<div class="logo">
<img src="logo.svg" alt="logo">
</div>
<a href="javascript:void(0);" id="icon" onclick="myBurger()">
<i class="fa fa-bars"></i>
</a>
<div class="navLinks" id="navLinks">
<a class="nav-link active" href="#">Home</a>
<a class="nav-link" href="#">Portfolio</a>
<a class="nav-link" href="#">Services</a>
<a class="nav-link" href="#">About</a>
<a class="nav-link" href="#">Contact</a>
</div>
</div>
Thank you