简体   繁体   English

我想切换一个移动菜单,我可以添加类但它不会删除它。 javascript

[英]I want to toggle a mobile menu, i can add the class but it doesn´t remove it. javascript

I try to toggle the mobile menu, but I can't get it to close.我尝试切换移动菜单,但无法关闭。 It doesn't remove the class when clicking the Close menu button.单击“关闭”菜单按钮时,它不会删除该类。

Adding works pretty fine.添加效果很好。 Is there something wrong with my if-statement?我的 if 语句有问题吗? Thank you for helping me.感谢你们对我的帮助。

Full code is on Github完整代码在 Github

 const openMenu = document.querySelector('.open-menu'); const closeMenu = document.querySelector('.close-menu'); const navList = document.querySelector('.nav-list'); const navBurger = document.querySelector('.nav-burger'); // Toggle Menu function toggleMenu() { if (navList.classList.contains('show-menu')) { navList.classList.remove('show-menu'); closeMenu.style.display = 'none'; openMenu.style.display = 'block'; } else { navList.classList.add('show-menu'); closeMenu.style.display = 'block'; openMenu.style.display = 'none'; } } navBurger.addEventListener('click', toggleMenu);
 <nav> <h3 class="nav-logo"><img class="logo-icon" src="https://www.coditt.com/images/LogoPlaceholder.png" alt="placeholdeer" style="width: 75px"/> Vacation</h3> <ul class="nav-list"> <li class="menu-item"><a href="#">Tours</a></li> <li class="menu-item"><a href="#">About us</a></li> <li class="menu-item"><a href="#">Contact</a></li> </ul> <a class="nav-burger" href="#"><img class="open-menu" alt="open menu" />Open menu</a> <a href="#" class="nav-burger"><img class="close-menu" alt="close menu" />Close menu</a> </nav>

Use navList.classList.toggle("show-menu") .使用navList.classList.toggle("show-menu")

So instead of:所以而不是:

function toggleMenu() {
    if (navList.classList.contains("show-menu")) {
        navList.classList.remove("show-menu");
        closeMenu.style.display = "none";
        openMenu.style.display = "block";
    } else {
        navList.classList.add("show-menu");
        closeMenu.style.display = "block";
        openMenu.style.display = "none";
    }
}

It should be:它应该是:

function toggleMenu() {
    navList.classList.toggle("show-menu");
}

EDIT:编辑:

As Raexune pointed out, my answer wasn't complete.正如 Raexune 指出的那样,我的回答并不完整。 So here is a better explanation..所以这里有一个更好的解释..

<a class="nav-burger" id="open" href="#"><img class="open-menu" src="./img/menu-svgrepo-com.svg" alt="open menu"></a>
            <a href="#" class="nav-burger" id="close"><img class="close-menu" src="./img/close-svgrepo-com.svg" alt="close menu"></a>

function toggleMenu() {
    navList.classList.toggle('show-menu')
    openMenu.style.display = 'none';
    closeMenu.style.display = 'block';
}

function untoggleMenu(){
    navList.classList.toggle('show-menu');
    openMenu.style.display = 'block';
    closeMenu.style.display = 'none';
}



document.getElementById('open').addEventListener('click', toggleMenu);
document.getElementById('close').addEventListener('click', untoggleMenu);

This isn't really the best answer, still.. but you can take some points.这仍然不是最好的答案,但你可以拿一些分数。 I used id instead of class , which should be better since class can return an array and so you have to indicate which instance you are manipulating.我使用id而不是class ,这应该会更好,因为class可以返回一个array ,所以你必须指出你正在操作哪个实例。 I believe that was the problem with the code not closing the menu.我相信这是代码没有关闭菜单的问题。

And second, there is a better way to use the functions that I used but I left it there to make it understandable as to how I manipulated these instances separately.其次,有一种更好的方法来使用我使用的函数,但我把它留在那里是为了便于理解我是如何分别操作这些实例的。

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

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