你可以通过点击链接来关闭汉堡菜单吗?

huangapple go评论112阅读模式
英文:

How can I close my hamburger menu by clicking on links?

问题

I've made this hamburger menu and it's working good except when I click on my links. It doesn't close the menu automatically, I always have to click the link then close the menu by the Hamburger button. How can I make the entire menu close when I click on one of my navbar links?

这个汉堡菜单已经制作好了,除了当我点击链接时它的工作正常。但是当我点击链接时,它不会自动关闭菜单,我总是不得不点击链接,然后通过汉堡按钮关闭菜单。我如何才能在点击导航栏链接时关闭整个菜单?

Here's my codepen: https://codepen.io/Softee/pen/dygGbWy

这是我的CodePen链接:https://codepen.io/Softee/pen/dygGbWy

英文:

I've made this hamburger menu and it's working good except when I click on my links. It doesn't close the menu automatically, I always have to click the link then close the menu by the Hamburger button.

How can I make the entire menu close when I click on one of my navbar links?

Here's my codepen: https://codepen.io/Softee/pen/dygGbWy
Be sure to be in responsive mobile viewport.

Thanks for your help.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const menu_btn = document.querySelector(&#39;.hamburger&#39;);
const mobile_menu = document.querySelector(&#39;.mobile-nav&#39;);

menu_btn.addEventListener(&#39;click&#39;, function () {
    menu_btn.classList.toggle(&#39;is-active&#39;);
    mobile_menu.classList.toggle(&#39;is-active&#39;);
});

<!-- language: lang-css -->

.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:&#39;&#39;;
    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;
  }
}

<!-- language: lang-html -->

 &lt;header&gt;
    &lt;div class=&quot;nav_container&quot;&gt;
      &lt;nav&gt;
        &lt;a href=&quot;#&quot;&gt;1&lt;/a&gt;
        &lt;a href=&quot;#&quot;&gt;2&lt;/a&gt;
        &lt;a href=&quot;#&quot;&gt;3&lt;/a&gt;
        &lt;a href=&quot;#&quot;&gt;4&lt;/a&gt;
        &lt;a href=&quot;#&quot;&gt;5&lt;/a&gt;
      &lt;/nav&gt;

      &lt;button class=&quot;hamburger&quot;&gt;
    &lt;div class=&quot;bar&quot;&gt;&lt;/div&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  &lt;/header&gt;

  &lt;nav class=&quot;mobile-nav&quot;&gt;
    &lt;a href=&quot;#&quot;&gt;1&lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;2&lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;3&lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;4&lt;/a&gt;
    &lt;a href=&quot;#&quot;&gt;5&lt;/a&gt;
  &lt;/nav&gt;

<!-- end snippet -->

答案1

得分: 1

以下是翻译好的部分:

"通常情况下,您不需要将此代码添加到您的代码中,因为导航链接会将用户带到一个新页面,菜单状态会被重置。

只有在您开发某种应用程序或者像这个页面上的固定内页导航那样使用时,这段代码才会真正有用:https://welcometounmh.runmytests.com/onboarding"

英文:

The answer to your question:

const menu_links = Array.from(document.querySelectorAll(&#39;.mobile-nav a&#39;));

menu_links.forEach(link =&gt; {
  link.addEventListener(&#39;click&#39;, function () {
      menu_btn.classList.remove(&#39;is-active&#39;);
      mobile_menu.classList.remove(&#39;is-active&#39;);
  });
})

Typically, you will not need to add this to your code because navigation links take a user to a new page where the menu state is reset.

This code is really only useful if you are working on some kind of application or have a sticky in-page navigation like on this page: https://welcometounmh.runmytests.com/onboarding

huangapple
  • 本文由 发表于 2023年4月13日 23:58:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定