function openNav() {
document.getElementById("mySidenav").style.width = "50%";
document.getElementById("mySidenav2").style.width = "50%";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("mySidenav2").style.width = "0";
}
<style>
body {
font-family: "Lato", sans-serif;
}
.leftSidenav {
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;
}
.leftSidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.leftSidenav a:hover, .offcanvas a:focus {
color: #f1f1f1;
}
.leftSidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
@media screen and (max-height: 450px) {
.leftSidenav {
padding-top: 15px;
}
.leftSidenav a {
font-size: 18px;
}
}
.rightSidenav {
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;
}
.rightSidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.rightSidenav a:hover, .offcanvas a:focus {
color: #f1f1f1;
}
.rightSidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
@media screen and (max-height: 450px) {
.rightSidenav {
padding-top: 15px;
}
.leftSidenav a {
font-size: 18px;
}
}
</style>
<div id="mySidenav" class="leftSidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="mySidenav2" class="rightSidenav">
<a href="#">About 2</a>
<a href="#">Services 2</a>
<a href="#">Clients 2</a>
<a href="#">Contact 2</a>
</div>
<h2>Would like toggle switch</h2>
<p>As of now open and close are 2 different icons</p>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
我在菜单由我的汉堡包图标激活的幻灯片。无论屏幕大小如何,我只使用汉堡包图标。截至目前,我的代码使用汉堡包图标在左右菜单中滑动。有一个单独的关闭图标来关闭菜单。我想使用汉堡包图标来打开和关闭我的菜单,并且如果可能的话,在菜单打开时将汉堡包图标变为X.
我试图做我的自我,我无法做到这一点。
谢谢你的帮助。
https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/ – Will