导航工作正常,但单击链接时应关闭 sidenav。因为它是一个单页。我怎样才能做到这一点?有人能帮助我吗。
代码:
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>
单击链接时,sidenav 应关闭