简体   繁体   English

第二次点击菜单不隐藏

[英]menu doesn't hide on second click

I'm facing a problem with a hamburger menu... When I click on the hamburger icon there is no problem, it opens and closes without any problem, also when I click on a link I am well redirected to the sections and the menu closes, no problem so far.我遇到了汉堡菜单的问题......当我点击汉堡图标时没有问题,它打开和关闭没有任何问题,当我点击链接时,我很好地重定向到部分和菜单关闭,目前没有问题。

But when i re open the menu and click a second time on a link section, my menu no longer wants to close and I can't figure it out why because i've put a condition, it must have a problem somewhere.. So please if you could help me to understand.但是当我重新打开菜单并再次单击链接部分时,我的菜单不再想关闭,我不知道为什么,因为我已经设置了一个条件,它一定有问题......所以如果你能帮助我理解,请。 Thank you very much.非常感谢。 Here is my code:这是我的代码:

html: html:

<header id="home" class="background-color">
<div id="logo">
        <a href="index.php#home"><img src="./img/logo.svg" alt="logo"></a>
    </div>
    <nav id="menu-dekstop">
        <a href="index.php#home">Accueil</a>
        <a href="index.php#projects">Projets</a>
        <a href="index.php#contact">Contact</a>
    </nav>
    <div class="show-menu">
        <div id="logo">
            <a href="index.php#home"><img src="./img/logo.svg" alt="logo"></a>
        </div>
    </div>
    <div id="menu-wrap">
        <nav class="menu">
            <a href="index.php#home">Accueil</a>
            <a href="index.php#projects">Projets</a>
            <a href="index.php#contact">Contact</a>
        </nav>
    </div>
    <div id ="menu-holder" class="menu-holder">
        <button id="open-menu" class="white">
            <div class="burger">
                <span></span>
            </div>
        </button>
    </div>
</header>

JavaScript JavaScript

burger.addEventListener('click', menuOpen)

function menuOpen()
{
    if (showMenu.style.display == 'block')
    {
        burger.classList.toggle('active');
        menuWrap.style.display = 'none';
        showMenu.style.display = 'none';
    }
    else 
    {
        burger.classList.toggle('active');
        menuWrap.style.display = 'block';
        showMenu.style.display = 'block';
    }
}

There appears to be no listener that closes the menu upon a link click.似乎没有在单击链接时关闭菜单的侦听器。

This code will accomplish a menu hide upon link click in the header:此代码将在 header 中的链接单击时完成菜单隐藏:

const header = document.getElementById('home');
header.addEventListener('click', function (evt) {
  const clickTarget = evt.target;
  if (clickTarget.tagName !== 'A") { return; }
  burger.classList.remove('active);
  menuWrap.style.display = 'none';
  showMenu.style.display = 'none';
});

Why did the menu get hidden the first time a link was clicked?为什么第一次单击链接时菜单会隐藏? Not sure, but I suspect it's because the first link click caused a page reload.不确定,但我怀疑这是因为第一次链接点击导致页面重新加载。

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

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