Hamburger Menu Open and Close JavaScript

Hamburger Menu Open and Close JavaScript
Project: Burger Menu with Vanilla JS
Author: Joseph Sayegh
Edit Online: View on CodePen
License: MIT

A hamburger menu is a commonly used navigational component for websites. It comes with a hamburger toggle button to open and close the menu. In this tutorial, you’ll learn how to create a simple hamburger menu with open and close effects using HTML, CSS, and JavaScript.

How to Create Hamburger Menu with Open and Close Effect in JavaScript

1. First of all, create a div element with a class name "burger-menu" and place an empty span tag inside it. We’ll style this div as an animated hamburger icon that will convert to a cross when the menu opens. Similarly, create another div element, define its id "menu", and place your navigation links inside it.

<div id="burger-menu">
  <span></span>
</div>

<div id="menu">
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About Us</a></li>
      <li><a href="#">Contact Us</a></li>
      <li><a href="#">News</a></li>
    </ul>
</div>

2. After that, style the hamburger menu using the following CSS styles. You can set the custom background, font-size, and dim overlay opacity according to your needs.

body{
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  font-family: sans-serif;
  background: #1e3c72 !important;
}

#burger-menu {
  cursor: pointer;
  height: 27px;
  width: 27px;
  margin: 50px;
  overflow: visible;
  position: relative;
  z-index: 2;
}
#burger-menu span,
#burger-menu span:before,
#burger-menu span:after {
  background: #fff;
  display: block;
  height: 2px;
  opacity: 1;
  position: absolute;
  transition: 0.3s ease-in-out;
}
#burger-menu span:before,
#burger-menu span:after {
  content: "";
}
#burger-menu span:before {
  left: 0px;
  top: -10px;
  width: 27px;
}
#burger-menu span {
  right: 0px;
  top: 13px;
  width: 27px;
}
#burger-menu span:after {
  left: 0px;
  top: 10px;
  width: 27px;
}
#burger-menu.close span:before {
  top: 0px;
  transform: rotate(90deg);
  width: 27px;
}
#burger-menu.close span {
  transform: rotate(-45deg);
  top: 13px;
  width: 27px;
}
#burger-menu.close span:after {
  top: 0px;
  left: 0;
  transform: rotate(90deg);
  opacity: 0;
  width: 0;
}

#menu {
  z-index: 1;
  min-width: 100%;
  min-height: 100%;
  position: fixed;
  top: 0;
  height: 0;
  visibility: hidden;
  opacity: 0;
  text-align: center;
  padding-top: 20px;
  transition: all 0.3s ease-in-out;
}
#menu.overlay {
  visibility: visible;
  opacity: 1;
  padding-top: 100px;
  background: rgba(0, 0, 0, 0.85);
}
#menu ul {
  padding: 0;
}
#menu li {
  list-style: none;
}
#menu a {
  color: #fff;
  display: block;
  font-size: 32px;
  margin-bottom: 30px;
  text-decoration: none;
}

3. Finally, get the hamburger menu by its id and attach a click event to toggle the “close” class in the JavaScript function.

var burgerMenu = document.getElementById('burger-menu');

var overlay = document.getElementById('menu');

burgerMenu.addEventListener('click', function() {
  this.classList.toggle("close");
  overlay.classList.toggle("overlay");
});

That’s all! hopefully, you have successfully created the hamburger menu. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *