Home > Blockchain >  JS Hamburger Menu not Collapsing back after Clicking menu item
JS Hamburger Menu not Collapsing back after Clicking menu item

Time:11-16

How to get this simple JS Hamburger menu to closed after a menu item has been clicked or selected. Also the possibility of a slide-in/slide-out animation with CSS. The animation is just a bonus, mainly just want the menu to hide automatically when an item has been selected.

function myFunction() {
  var x = document.getElementById("myLinks");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}
.topnav {
  overflow: hidden;
  background-color: #333;
  position: relative;
}


.topnav #myLinks {
  display: none;
}


.topnav a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

/* Style the hamburger menu */
.topnav a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;
}


.topnav a:hover {
  background-color: #ddd;
  color: black;
}


.active {
  background-color: #04AA6D;
  color: white;
}
<div class="topnav">
  <a href="#home" class="active">Logo</a>
 
  <div id="myLinks">
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
  </div>
 
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
   
  </a>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use onclick-events. I added some to the a menuitems

Here's the code:

var x = document.getElementById("myLinks");

function myFunction() {
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}

function closeNav() {
  x.style.display = "none";
}
.topnav {
  overflow: hidden;
  background-color: #333;
  position: relative;
}


.topnav #myLinks {
  display: none;
}


.topnav a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

/* Style the hamburger menu */
.topnav a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;
}


.topnav a:hover {
  background-color: #ddd;
  color: black;
}


.active {
  background-color: #04AA6D;
  color: white;
}
<div class="topnav">
  <a href="#home" class="active">Logo</a>
 
  <div id="myLinks">
    <a href="#news" onclick="closeNav();">News</a>
    <a href="#contact" onclick="closeNav();">Contact</a>
    <a href="#about" onclick="closeNav();">About</a>
  </div>
 
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
   
  </a>
</div>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You should toggle the .active class by using:

    function myFunction() {
  var element = document.getElementById("myDIV");
  element.classList.toggle("mystyle");
}

By using this you will be able to remove or add same class just clicking on the element (you should call this function on click event)

  • Related