gpt4 book ai didi

javascript - 侧面导航的菜单图标

转载 作者:行者123 更新时间:2023-11-28 04:08:50 26 4
gpt4 key购买 nike

菜单图标单击后会打开侧面导航,并且图标会变成“X”。第二次单击时,我希望它关闭侧面导航并返回到正常菜单图标。

我有以下代码:

<!DOCTYPE html>
<html>
<head>
<style>
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}

.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}

.sidenav a:hover {
color: #f1f1f1;
}

.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

#main {
transition: margin-left .5s;
padding: 16px;
}

@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}

.container {
display: inline-block;
cursor: pointer;
}

.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}

.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px) ;
transform: rotate(-45deg) translate(-9px, 6px) ;
}

.change .bar2 {opacity: 0;}

.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px) ;
transform: rotate(45deg) translate(-8px, -8px) ;
}

</style>

</head>
<body>

<div id="mySidenav" class="sidenav">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>

<div id="main">
<div class="container" style="font-size:30px;cursor:pointer" onclick="openNav(); myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</div>

<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
}

function myFunction(x) {
x.classList.toggle("change");
}


</script>

</body>
</html>

它目前可以很好地打开侧面导航,但我不确定如何告诉它在第二次单击时关闭侧面导航,或者在哪里放置我的 closeNav() 函数。

如有任何帮助,我们将不胜感激。

最佳答案

如果 JQuery 尚未使用,我不喜欢添加它...您只需检查菜单的状态即可做到这一点。

<div id="mySidenav" class="sidenav">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>

<div id="main">
<div class="container" style="font-size:30px;cursor:pointer" onclick="toggleMenu(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</div>

<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
}

function toggleMenu(x) {
x.classList.toggle("change");
if(document.getElementById("mySidenav").style.width > "1px"){
closeNav();
} else {
openNav();
}
}
</script>

关于javascript - 侧面导航的菜单图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46473467/

26 4 0