我想要一个按钮来打开
和关闭
。
这就是我用于侧面菜单的关闭和打开
:
function openNav() {
document.getElementById("mySidenav").style.width = "300px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
这是打开的按钮
<span class="collapses" style="font-size:30px;cursor:pointer;float:right;color:
#fff;margin-top: 20px" onclick="openNav()">☰</span>
这是关闭按钮
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
最佳答案
使用 ternary operator 根据当前宽度值切换宽度.
function openNav() {
var div = document.getElementById("mySidenav");
div.style.width = div.style.width != '300px' ? "300px" : 0;
}
关于javascript - 关闭和打开按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41055103/