Animated Hamburger Menu With jQuery And CSS3

Animated Hamburger Menu With jQuery And CSS3
File Size: 6.02 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A dead simple jQuery hamburger menu plugin for both mobile and desktop that reveals a dropdown menu with a fullscreen background overlay as you click/tap on the toggle icon.

How to use it:

1. Create the site menu and hamburger menu toggle button in the web page.

<div class="menu">
  <div class="hamburger-menu-wrapper">
    <button class="hamburger-menu">
      <span>toggle menu</span>
    </button>
  </div>
  <div class="menu-list">
    <a href="#">Link 1</a><br>
    <a href="#">Link 2</a><br>
    <a href="#">Link 3</a><br>
    <a href="#">Link 4</a><br>
  </div>
</div>

2. Create an overlay element for the hamburger menu when opened.

<div class="menu-overlay"></div>

3. Style & animate the toggle button as the hamburger menu is opened and closed.

.hamburger-menu-wrapper {
  margin-top: 40px;
  background: #323232;
  padding: 10px;
  display: inline-block;
}

.hamburger-menu-wrapper.bounce-effect { animation: bounce 0.3s ease 1; }

.hamburger-menu {
  border: 0;
  margin: 0 auto;
  display: block;
  position: relative;
  overflow: hidden;
  padding: 0;
  width: 36px;
  height: 36px;
  font-size: 0;
  text-indent: -9999px;
  cursor: pointer;
  z-index: 9999;
  cursor: pointer;
  background: transparent;
}

.hamburger-menu:focus { outline: none; }

.hamburger-menu span {
  display: block;
  position: absolute;
  top: 17px;
  left: 5px;
  right: 5px;
  height: 2px;
  background: #02D5FD;
}

.hamburger-menu span:before, .hamburger-menu span:after {
  position: absolute;
  display: block;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #02D5FD;
  content: "";
}

.hamburger-menu span:before { top: -7px; }

.hamburger-menu span:after { bottom: -7px; }

.hamburger-menu span:before, .hamburger-menu span:after {
  transition-duration: 0.3s, 0.3s;
  transition-delay: 0.3s, 0s;
}

.hamburger-menu span:before { transition-property: top, transform; }

.hamburger-menu span::after { transition-property: bottom, transform; }

.hamburger-menu.active span { background: none; }

.hamburger-menu.active span:before {
  top: 0;
  transform: rotate(225deg);
}

.hamburger-menu.active span:after {
  bottom: 0;
  transform: rotate(135deg);
}

.hamburger-menu.active span:before, .hamburger-menu.active span:after { transition-delay: 0s, 0.3s; }

@keyframes 
bounce { 0% {
 transform: rotate(0);
}
 45% {
 transform: rotate(15deg);
}
 90% {
 transform: rotate(-7deg);
}
 100% {
 transform: rotate(0);
}
}

4. Style the hamburger dropdown menu in your CSS.

.menu {
  position: relative;
  min-width: 220px;
  text-align: center;
}

.menu-list {
  display: none;
  position: absolute;
  top: calc(100% + 40px);
  width: 100%;
  text-align: center;
  z-index: 9999;
}

.menu-list a {
  color: #C6D2D6;
  text-decoration: none;
  font-size: 18px;
  display: inline-block;
  margin: 15px 0;
  transition: all 0.5s ease;
}

.menu-list a:hover { color: #02D5FD; }

.menu-overlay {
  transition: margin 300ms cubic-bezier(0.17, 0.04, 0.03, 0.94);
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1111;
}

5. Apply your own CSS styles to the background overlay.

.menu-overlay {
  transition: margin 300ms cubic-bezier(0.17, 0.04, 0.03, 0.94);
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1111;
}

6. Put the necessary jQuery JavaScript library at the bottom of the web page.

<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>

7. The core jQuery script to enable the hamburger menu.

(function() {
  'use strict';
  $('.hamburger-menu').click(function (e) {
      e.preventDefault();
      if ($(this).hasClass('active')){
          $(this).removeClass('active');
          $('.menu-overlay').fadeToggle( 'fast', 'linear' );
          $('.menu .menu-list').slideToggle( 'slow', 'swing' );
          $('.hamburger-menu-wrapper').toggleClass('bounce-effect');
      } else {
          $(this).addClass('active');
          $('.menu-overlay').fadeToggle( 'fast', 'linear' );
          $('.menu .menu-list').slideToggle( 'slow', 'swing' );
          $('.hamburger-menu-wrapper').toggleClass('bounce-effect');
      }
  })
})();

This awesome jQuery plugin is developed by agilie. For more Advanced Usages, please check the demo page or visit the official website.