Expanding Toggle Menu With jQuery And CSS/CSS3

Expanding Toggle Menu With jQuery And CSS/CSS3
File Size: 2.04 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A pretty cool toggle menu concept that expands the hamburger button into a horizontal (or vertical on mobile device) navigation menu when toggled. Built with jQuery, CSS/CSS3 and plain HTML.

How to use it:

1. Create the expanding toggle menu from a normal html list as these:

<ul>
  <li>
    <div id="menu-button">
      <div class="bar"></div>
      <div class="bar"></div>
      <div class="bar"></div>
    </div>
  </li>
  <li>Home</li>
  <li>Plugins</li>
  <li>About Us</li>
  <li>Contact</li>
</ul>

2. The primary CSS/CSS3 for the menu.

ul {
  width: 460px;
  height: 60px;
  background: #fff;
  border-radius: 5px;
  overflow: hidden;
  position: relative;
}

ul li {
  width: 100px;
  height: 60px;
  background: #fff;
  position: absolute;
  left: 0;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-shadow: 15px 0 30px -5px rgba(0, 0, 0, 0.2);
  box-shadow: 15px 0 30px -5px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  font-family: 'Roboto', sans-serif;
  padding-left: 15px;
  color: #5B8CFF;
  opacity: 0;
}

ul li:first-child {
  padding-left: 0;
  z-index: 10;
  opacity: 1;
  width: 60px;
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

ul li:after {
  content: '';
  display: inline-block;
  position: absolute;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 30px 0 30px 15px;
  border-color: transparent transparent transparent #fff;
  right: -15px;
  top: 0;
}

3. Style & animate the hamburger toggle button.

#menu-button .bar {
  width: 20px;
  height: 2px;
  background: #5B8CFF;
  -webkit-transition: 0.3s linear;
  transition: 0.3s linear;
}

#menu-button .bar:nth-child(2) { margin: 5px 0; }

#menu-button.active .bar:first-child {
  -webkit-animation: barOne 0.4s ease-in-out forwards;
  animation: barOne 0.4s ease-in-out forwards;
}

@-webkit-keyframes 
barOne {  0% {
 -webkit-transform: none;
 transform: none;
}
 25% {
 -webkit-transform: translateY(7px);
 transform: translateY(7px);
}
 100% {
 -webkit-transform: translateY(7px) rotate(45deg);
 transform: translateY(7px) rotate(45deg);
}
}

@keyframes 
barOne {  0% {
 -webkit-transform: none;
 transform: none;
}
 25% {
 -webkit-transform: translateY(7px);
 transform: translateY(7px);
}
 100% {
 -webkit-transform: translateY(7px) rotate(45deg);
 transform: translateY(7px) rotate(45deg);
}
}

#menu-button.active .bar:nth-child(2) {
  -webkit-transform: scale(0);
  transform: scale(0);
}

#menu-button.active .bar:last-child {
  -webkit-animation: barTwo 0.4s ease-in-out forwards;
  animation: barTwo 0.4s ease-in-out forwards;
}

@-webkit-keyframes 
barTwo {  0% {
 -webkit-transform: none;
 transform: none;
}
 25% {
 -webkit-transform: translateY(-7px);
 transform: translateY(-7px);
}
 100% {
 -webkit-transform: translateY(-7px) rotate(-45deg);
 transform: translateY(-7px) rotate(-45deg);
}
}

@keyframes 
barTwo {  0% {
 -webkit-transform: none;
 transform: none;
}
 25% {
 -webkit-transform: translateY(-7px);
 transform: translateY(-7px);
}
 100% {
 -webkit-transform: translateY(-7px) rotate(-45deg);
 transform: translateY(-7px) rotate(-45deg);
}
}

4. Rotate the navigation menu when the screen size is smaller than 468px:

@media screen and (max-width: 467px) {

ul {
  -webkit-transform: rotate(90deg);
  transform: rotate(90deg);
}
}

5. Load the necessary jQuery JavaScript library at the end of the page.

<script src="https://code.jquery.com/jquery-3.3.1.min.js" 
        integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" 
        crossorigin="anonymous">
</script> 

6. The main script to activate the toggle menu.

let menu = $('li:first-child'),
    menuButton = $('#menu-button'),
    Home = $('li:nth-child(2)'),
    Plugins = $('li:nth-child(3)'),
    about = $('li:nth-child(4)'),
    contact = $('li:nth-child(5)');

menu.on('click',() => {
  menuButton.toggleClass('active');
  if(menuButton.hasClass('active')){
    Home.animate({'left':'110px','opacity':'1','z-index':'8'},500);
    Plugins.animate({'left':'210px','opacity':'1','z-index':'6'},500);
    about.animate({'left':'310px','opacity':'1','z-index':'4'},500);
    contact.animate({'left':'410px','opacity':'1','z-index':'2'},500);
  }
  else {
    Home.animate({'left':'0','opacity':'0'},500);
    Plugins.animate({'left':'0','opacity':'0'},500);
    about.animate({'left':'0','opacity':'0'},500);
    contact.animate({'left':'0','opacity':'0'},500);
  }
});

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