简体   繁体   English

如何折叠响应式导航栏

[英]How to collapse a responsive navigation bar

So I have an issue in collapsing my navigation menu on Android when user clicks on the link in the nav bar in my website.因此,当用户单击我网站导航栏中的链接时,我在 Android 上折叠我的导航菜单时遇到问题。 I tried using addEventListener but that doesn't work.我尝试使用addEventListener但这不起作用。 So in other words I mean that I want to collapse/close when scroll or click on the nav links.所以换句话说,我的意思是我想在滚动或单击导航链接时折叠/关闭。 I don't want to use any library Here are the codes我不想使用任何库这里是代码

const hamburger = document.querySelector('.nav-menu');
const navLinks = document.querySelector('.nav-links');
const links = document.querySelector('.nav-links li');
hamburger.addEventListener('click', () => {
  navLinks.classList.toggle('activate');
  links.forEach(link => {
  })
})
//Here is the scroll one
//This part is not working 
hamburger.addEventListener('scroll', 'click', () => {
  navLinks.classList.remove('activate');
})

Here is my responsive nav menu css code这是我的响应式导航菜单 css 代码

@media screen and (max-width: 768px) {
  .nav-line {
    height: 3px;
    width: 30px;
    background: white;
    margin: 5px;
  }
  nav {
    position: relative;
  }
  .nav-menu {
    position: absolute;
    cursor: pointer;
    right: 5%;
    top: 50%;
    transform: translate(-5%, -50%);
    z-index: 2;
  }
  .nav-links {
  position: fixed;
  background: var(--gray);
  height: 100vh;
  width: 100%;
  flex-direction: column;
  z-index: 1;
  clip-path: circle(100px at 90% -10%);
  -webkit-clip-path: circle(100px at 90% -10%);
  transition: all 1s ease-out;
  pointer-events: none;
  /*display: none;*/
}
.nav-links.activate{
  clip-path: circle(1000px at 90% -10%);
  -webkit-clip-path: circle(1000px at 90% -10%);
  pointer-events: all;
}
.nav-menu {
  flex-direction: column;
}
.nav-links:hover {
  color: red;
}
}

Here is my html part这是我的 html 零件

  <div class="nav-menu">
    <div class="nav-line"></div>
<div class="nav-line"></div>
<div class="nav-line"></div>
  </div>

  <ul class="nav-links">
      <li><a href="#">Home</a></li>
      <li><a href="#about">About Me</a></li>
    <li><a href="#">Tools</a></li>
    <li><a href="#">Contact Me</a></li>
  </ul>
</nav>

The navbar closes perfectly when clicked on humburger button like open while clicking it.当点击汉堡按钮时,导航栏会完美关闭,就像点击它时打开一样。 It is possible to do with same animation like the navbar closes while clicking on nav links or scrolling?是否可以使用相同的 animation,例如在单击导航链接或滚动时关闭导航栏? Here is the website link: https://ishankbg.tech If I need to add more details I will If you feel this question is not good or missing something you can downvote这是网站链接: https://ishankbg.tech如果我需要添加更多详细信息,我会

set the event listener for scroll separately to document object, since mouse may not be over the menu when the user scrolls.将滚动的事件侦听器单独设置到文档 object,因为当用户滚动时鼠标可能不在菜单上。

as: document.addEventListener('scroll', #function remove classlist#)如:document.addEventListener('scroll', #function remove classlist#)

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

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