我的关闭导航功能在单击时不起作用

我试图在单击菜单图标时使 left=0vw 但它不起作用。CSS 和 html 工作得很好我是新手,如果它如此明显,不要取笑 :'(


var menuEl = document.getElementsByClassName("bx-menu");

var navEl = document.getElementsByClassName("nav");

function closenav(){

    if(navEl.style.left === "-100vw"){

        navEl.style.left = "0vw";

    } else {

        navEl.style.left = "-100vw";

    }

}


menuEl.addEventListener("click",closenav());


慕姐8265434
浏览 172回答 3
3回答

慕斯709654

document.getElementsByClassName 返回具有给定类名的所有元素的数组。要获得可能是您想要的第一个元素,您需要选择此数组的第一个元素。还要注意下面传递的函数。这是您更改后的代码var menuEl = document.getElementsByClassName("bx-menu")[0];var navEl = document.getElementsByClassName("nav")[0];function closenav(){    if(navEl.style.left === "-100vw"){        navEl.style.left = "0vw";    } else {        navEl.style.left = "-100vw";    }}menuEl.addEventListener("click",closenav);// Note you need to pass the function here not call the function so I’ve removed the ()var menuEl = document.getElementsByClassName("bx-menu");var navEl = document.getElementsByClassName("nav");function closenav(){    if(navEl.style.left === "-100vw"){        navEl.style.left = "0vw";    } else {        navEl.style.left = "-100vw";    }}// 你需要这个两次吗??如果是这样再次删除 () menuEl.addEventListener("click",closenav());如果导航栏是唯一的,您可能想给它一个唯一的 ID,然后您可以使用 document.getElementById 选择它

森林海

具体看document.getElementsByClassName("bx-menu")[0],我添加了[0]作为getElementsByClassName返回的类的出现数组。closenav 还要确保在像这样将它添加为事件监听器时删除括号:menuEl.addEventListener("click", closenav);或者像我在下面的代码片段中所做的那样将整个函数封装在事件监听器中。var menuEl = document.getElementsByClassName("bx-menu")[0];menuEl.addEventListener("click", function (event) {&nbsp; &nbsp; var navEl = document.getElementsByClassName("nav")[0];&nbsp; &nbsp; if(navEl.style.left === "-100vw"){&nbsp; &nbsp; &nbsp; &nbsp; navEl.style.left = "0vw";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; navEl.style.left = "-100vw";&nbsp; &nbsp; }});.nav {&nbsp; background-color: blue;&nbsp; height: 100px;&nbsp; width: 100px;&nbsp; position: absolute;}<div class="bx-menu">Click here!</div><div class="nav" style="left: -100vw;"></div>

噜噜哒

一个建议是使用#id而不是.class通过使用来获取唯一元素getElementById。否则,如果您使用getElementsByClass.我还建议:a) 使用transform: translate而不是动画,left因为 left 会导致重新计算所有元素,而 transform 只考虑移动元素,并且b) 切换一个类而navEl.classList.toggle()不是在你的方法中查找样式属性。不要介意CSS。这主要是为了设置东西。let navEl = document.getElementById("nav");let hamburgerEl = document.getElementById("hamburger");hamburgerEl.addEventListener("click", toggleMenu);function toggleMenu() {&nbsp; navEl.classList.toggle("open");}#nav {&nbsp; position: fixed;&nbsp; left: 0px;&nbsp; top: 0px;&nbsp; width: 200px;&nbsp; height: 400px;&nbsp;&nbsp;&nbsp; padding: 1rem; /* 'rem' is the size of the font in the body element */&nbsp; text-align: center;&nbsp; background-color: lightblue;&nbsp; border-right: 1px solid darkgrey;&nbsp;&nbsp;&nbsp; transform: translateX(-100%);&nbsp; transition: transform 400ms;}#nav.open { /* the event listener toggles this class */&nbsp; transform: translateX(0px);}#hamburger {&nbsp; position: relative; /* in order to use z-index to place #hamburger over the menu */&nbsp; z-index: 100;&nbsp; width: 20px;&nbsp; height: 20px;&nbsp;&nbsp;&nbsp; display: flex;&nbsp; /* to center the strokes */&nbsp; align-items: center;&nbsp;&nbsp;&nbsp; cursor: pointer;}#strokes,#strokes::before,#strokes::after{&nbsp; width: 100%;&nbsp; height: 2px;&nbsp; background-color: black;}#strokes::before, /* pseudo-elements */#strokes::after {&nbsp; content: '';&nbsp; display: block;}#strokes::before {&nbsp; transform: translateY(-6px);}#strokes::after {&nbsp; transform: translateY(4px);}<body><div id="nav">MENU</div><div id="hamburger">&nbsp; <div id="strokes"></div></div></body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript