我正在学习JS,并且制作了可折叠的导航栏。单击按钮后,我打开了菜单,但不会关闭。我已经在该站点上搜索了答案,但是发现的所有内容都涉及Bootstrap。这只是纯JS。我还花了几个小时检查代码,以发现语法错误或任何可能出错的地方。任何帮助将不胜感激!
这是我的代码:
let toggleNavStatus = false;
let toggleNav = function() {
let getSidebar = document.querySelector(".nav-sidebar");
let getSidebarUl = document.querySelector(".nav-sidebar ul");
let getSidebarTitle = document.querySelector(".nav-sidebar span");
let getSidebarLinks = document.querySelectorAll(".nav-sidebar a");
if (toggleNavStatus === false) {
getSidebarUl.style.visibility = "visible";
getSidebar.style.width = "315px";
getSidebarTitle.style.opacity = "0.5";
let arrayLength = getSidebarLinks.length;
for (i = 0; i < array.length; i++) {
getSidebarLinks[i].style.opacity = "1";
}
toggleNavStatus = true;
}
else if (toggleNavStatus === true) {
getSidebar.style.width = "50px";
getSidebarTitle.style.opacity = "0";
let arrayLength = getSidebarLinks.length;
for (i = 0; i < array.length; i++) {
getSidebarLinks[i].style.opacity = "0";
}
getSidebarUl.style.visibility = "hidden";
toggleNavStatus = false;
}
}