[英]my close nav function doesnt work onclick
I'm trying to make left= 0vw on click of menu icon but it doesn't work .我试图在单击菜单图标时使 left= 0vw 但它不起作用。 CSS and html work pretty well I'm a newbie don't make fun if it's so obvious :'( 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());
You are using getElementsByClassName() method - it gets element by its class attribute and index.您正在使用getElementsByClassName()方法 - 它通过其类属性和索引获取元素。
Specify what element's index you want to target.指定要定位的元素索引。
var menuEl = document.getElementsByClassName("bx-menu")[0];
var navEl = document.getElementsByClassName("nav")][0];
think he cant read the value of -100vw in line 4认为他无法读取第 4 行中 -100vw 的值
so you have to read out the css value with this:所以你必须用这个读出css值:
const element = document.querySelector('.element')
const style = getComputedStyle(element)
document.getElementsByClassName returns an array of all the elements that have the given class name. document.getElementsByClassName 返回具有给定类名的所有元素的数组。
To get the first element which is probably the one that you want you need to choose the first element of this array.要获得可能是您想要的第一个元素,您需要选择此数组的第一个元素。
Note also the passing of a function in the below.还要注意下面的函数传递。
Here is your altered code这是您更改的代码
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";
}
}
// do you need this twice?? // 你需要两次吗?? If so again remove the () menuEl.addEventListener("click",closenav());如果是这样再次删除 () menuEl.addEventListener("click",closenav());
If the nav bar is unique you may like to look at giving it a unique id and then you can select it using document.getElementById如果导航栏是唯一的,您可能想查看给它一个唯一的 id,然后您可以使用 document.getElementById 选择它
Look specifically at document.getElementsByClassName("bx-menu")[0]
, I've added [0]
as the getElementsByClassName
returns an array of occurences of the class.特别查看document.getElementsByClassName("bx-menu")[0]
,我添加了[0]
作为getElementsByClassName
返回类出现的数组。
Also be sure to remove the brackets of closenav
when adding it as an eventlistener like so: menuEl.addEventListener("click", closenav);
在将其添加为closenav
时,请务必删除closenav
的括号, closenav
: menuEl.addEventListener("click", closenav);
or encapsulate the whole function inside the eventlistener like I did in the snippet below.或者像我在下面的代码片段中所做的那样,将整个函数封装在事件侦听器中。
var menuEl = document.getElementsByClassName("bx-menu")[0]; menuEl.addEventListener("click", function (event) { var navEl = document.getElementsByClassName("nav")[0]; if(navEl.style.left === "-100vw"){ navEl.style.left = "0vw"; } else { navEl.style.left = "-100vw"; } });
.nav { background-color: blue; height: 100px; width: 100px; position: absolute; }
<div class="bx-menu"> Click here! </div> <div class="nav" style="left: -100vw;"> </div>
An advice is to use #id
instead of .class
to get the unique elements by using getElementById
.一个建议是使用#id
而不是.class
通过使用getElementById
来获取唯一元素。 Otherwise you will end up with an array of elements, if you're using getElementsByClass
.否则,如果您使用的是getElementsByClass
,您最终会得到一个元素数组。
I would also suggest to:我还建议:
a) use transform: translate
instead of animating with left
because left causes recalculation all elements, while transform only takes the moving element into consideration, and a) 使用transform: translate
而不是使用left
动画,因为 left 会导致重新计算所有元素,而 transform 只考虑移动元素,并且
b) toggle a class with navEl.classList.toggle()
instead of looking up style properties in your method. b) 使用navEl.classList.toggle()
) 切换类,而不是在您的方法中查找样式属性。
Don't mind the CSS.不要介意CSS。 It's mostly to set things up.这主要是为了设置东西。
let navEl = document.getElementById("nav"); let hamburgerEl = document.getElementById("hamburger"); hamburgerEl.addEventListener("click", toggleMenu); function toggleMenu() { navEl.classList.toggle("open"); }
#nav { position: fixed; left: 0px; top: 0px; width: 200px; height: 400px; padding: 1rem; /* 'rem' is the size of the font in the body element */ text-align: center; background-color: lightblue; border-right: 1px solid darkgrey; transform: translateX(-100%); transition: transform 400ms; } #nav.open { /* the event listener toggles this class */ transform: translateX(0px); } #hamburger { position: relative; /* in order to use z-index to place #hamburger over the menu */ z-index: 100; width: 20px; height: 20px; display: flex; /* to center the strokes */ align-items: center; cursor: pointer; } #strokes, #strokes::before, #strokes::after { width: 100%; height: 2px; background-color: black; } #strokes::before, /* pseudo-elements */ #strokes::after { content: ''; display: block; } #strokes::before { transform: translateY(-6px); } #strokes::after { transform: translateY(4px); }
<body> <div id="nav"> MENU </div> <div id="hamburger"> <div id="strokes"></div> </div> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.