我制作了这个汉堡包菜单,除了我点击链接外,它运行得很好.它不会自动关闭菜单,我总是必须点击链接,然后通过汉堡包按钮关闭菜单.

当我点击导航栏中的一个链接时,如何关闭整个菜单?

这是我的助手:https://codepen.io/Softee/pen/dygGbWy人 请确保处于响应式移动视区中.

谢谢你的帮助.

const menu_btn = document.querySelector('.hamburger');
const mobile_menu = document.querySelector('.mobile-nav');

menu_btn.addEventListener('click', function () {
    menu_btn.classList.toggle('is-active');
    mobile_menu.classList.toggle('is-active');
});
.hamburger{
    position:relative;
    margin-top: 30px;
    margin-left: 50px;
    display:block;
    width:35px;
    cursor:pointer;
    appearance:none;
    background:none;
    outline:none;
    border:none
}

.hamburger .bar,.hamburger:after,.hamburger:before{
    content:'';
    display:block;
    width:100%;
    height:5px;
    background-color:#1B1B1B;
    margin:6px 0px;
    transition:0.4s
}

.hamburger.is-active:before{
    transform:rotate(-45deg) translate(-8px, 6px);
    background-color: #1B1B1B;
}

.hamburger.is-active:after{
    transform:rotate(45deg) translate(-9px, -8px);
    background-color: #1B1B1B;
}

.hamburger.is-active .bar{
    opacity:0;
}

.mobile-nav{
    position:fixed;
    top:0;
    left:100%;
    width:100%;
    min-height:100vh;
    display:block;
    background: pink;
    padding-top:120px;
    transition:0.4s
}

.mobile-nav.is-active{
    left:0
}

.mobile-nav a{
    font-family: hagincaps;
    font-size: 32px;
    display:block;
    margin:0 auto 16px;
    text-align:center;
    padding:16px 16px;
    color:#1B1B1B;
    text-decoration:none
}

header .nav_container{
    z-index: 8;
    position: fixed;
    height: 100px;
    width: 100%;
    align-items: center;
}

header .nav_container nav{
    padding-top:32px;
    width: 900px;
    margin: 0 auto;
    display: flex;
    justify-content: space-evenly;
}

header .nav_container nav a{
    font-family: hagincaps;
    text-transform: uppercase;
    font-size:28px;
    color:757575;
    text-decoration:none
}

header .nav_container nav a:hover{
    border-bottom: 5px solid white;
    border-radius: 10px;
    color: white;
}

@media (min-width: 768px){
    .mobile-nav{
        display:none
    }
    .hamburger{
        display:none
    }
}

@media (max-width: 767px){
    header .nav_container nav{
        display:none
    }
}

@media (max-width: 855px){
  header .nav_container nav{
    width: 800px;
    margin: 0 auto;
  }
}
 <header>
    <div class="nav_container">
      <nav>
        <a href="#">1</a>
        <a href="#">2</a>
        <a href="#">3</a>
        <a href="#">4</a>
        <a href="#">5</a>
      </nav>

      <button class="hamburger">
    <div class="bar"></div>
      </button>
    </div>
  </header>

  <nav class="mobile-nav">
    <a href="#">1</a>
    <a href="#">2</a>
    <a href="#">3</a>
    <a href="#">4</a>
    <a href="#">5</a>
  </nav>

推荐答案

你的问题的答案是:

const menu_links = Array.from(document.querySelectorAll('.mobile-nav a'));

menu_links.forEach(link => {
  link.addEventListener('click', function () {
      menu_btn.classList.remove('is-active');
      mobile_menu.classList.remove('is-active');
  });
})

通常,您不需要将其添加到代码中,因为导航链接会将用户带到菜单状态重置的新页面.

只有当您正在使用某种应用程序或有一个粘性的页面内导航时,此代码才真正有用:https://welcometounmh.runmytests.com/onboarding

Javascript相关问答推荐

使用事件监听器在JavaScript中重新分配全局变量

Python类实现的JavaScript吊坠是什么样子的?

在Phaser中查找哪个物体处于碰撞中

Toast函数找不到其dis

在JS中获取名字和姓氏的首字母

Google Apps脚本中的discord邀请API响应的日期解析问题

react—router v6:路由没有路径

将现场录音发送到后端

如何使onPaste事件与可拖动的HTML元素一起工作?

如何控制Reaction路由加载器中的错误状态?

我正在建立一个基于文本的游戏在react ,我是从JS转换.我怎样才能使变量变呢?

无法使用单击按钮时的useState将数据从一个页面传递到另一个页面

使用ThreeJ渲染的形状具有抖动/模糊的边缘

如何在每次单击按钮时重新加载HighChart/设置HighChart动画?

查询参数未在我的Next.js应用路由接口中定义

如何将zoom 变换应用到我的d3力有向图?

在Odoo中如何以编程方式在POS中添加产品

如何组合Multer上传?

每隔3个项目交替显示,然后每1个项目交替显示

表单数据中未定义的数组键