简体   繁体   English

使用 querySelector 获取导航以显示和隐藏并显示无/块?

[英]Get nav to show and hide with querySelector and display none/block?

Im trying to make a navbar that shows and hides when you press a button.我试图制作一个在您按下按钮时显示和隐藏的导航栏。 I get it to show but not hide.我让它显示但不隐藏。 If anyone know how i can add a litte fade animation when its shows please let me know.如果有人知道我如何在它的节目中添加一点褪色 animation,请告诉我。

I have used js and querySelector to get it to shoew.我已经使用 js 和 querySelector 将其固定。

let button = document.getElementById("button");

let press = document.querySelector('nav').style.display = 'none';
press = true;

button.onclick = () => {

    if(press == true){
        document.querySelector('nav').style.display = 'block';
    }
    else  if(press == false){
        document.querySelectorAll('nav').style.display = 'none';
    }
}

This makes the nav to show but it wont hide again.这使得导航显示,但它不会再次隐藏。

Update your press value for the next click.为下一次点击更新您的press价值。

    if(press == true){
        document.querySelector('nav').style.display = 'block';
    } else {
        document.querySelectorAll('nav').style.display = 'none';
    }
    press = !press;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM