Everything is working smoothly with the navigation, but there's a small glitch to address: the side navigation bar should close automatically when a link is clicked on this one-page website. Can anyone assist me with this issue?
Here is the code snippet:
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
right: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a id="nav" href="#about">About</a>
<a id="nav" href="#service">Services</a>
<a id="nav" href="#contact">contact</a>
</div>
<p>Click to open</p>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
The desired behavior is for the sidenav to close automatically upon clicking a link.