Simple Mobile Menu

by Kevin Pliester

HTML

<div id="mobile-shadow"></div>
<div id="mobile-button">
  <span class="bar top-bar"></span>
  <span class="bar mid-bar"></span>
  <span class="bar bot-bar"></span>
</div>

<div id="navigation">
</div>

<div id="mobile-menu">
  <div class="widget">
    <ul class="mobile-menu">
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
    </ul>
  </div>
</div>

CSS

body,
html {
  color: #333;
  background-color: #fff;
}

body.open,
html.open {
  overflow: hidden;
}

#mobile-shadow {
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: 150;
  background-color: rgba(0, 0, 0, .4);
  display: none;
}

#mobile-shadow.open {
  display: block;
}

#navigation {
  background-color: #3498db;
  width: 100%;
  position: relative;
  z-index: 100;
  height: 250px;
}

#mobile-button {
  position: absolute;
  z-index: 300;
  text-align: center;
  cursor: pointer;
  color: #fff;
  transition: all 0.2s;
}

#mobile-menu {
  position: fixed;
  z-index: 200;
  top: 0;
  background: #333;
  color: #fff;
  height: 100%;
  padding-top: 50px;
  font-family: arial;
  left: -260px;
  width: 260px;
  transition: all .2s linear;
}

#mobile-menu.open-left {
  left: 0px;
}

#mobile-menu.open-right {
  left: 0px;
}

#mobile-menu .mobile-menu {
  list-style: none;
  margin: 0;
  padding: 0;
}

#mobile-menu .mobile-menu a {
  display: block;
  padding: 10px 15px;
  text-decoration: none;
  color: #fff;
}

#mobile-menu.open {
  left: 0px;
}

#mobile-button {
  color: #fff;
  padding: 15px;
}

#mobile-button .bar {
  display: block;
  width: 22px;
  height: 2px;
  background-color: #fff;
  transition: all 0.2s;
}

#mobile-button .bar+.bar {
  margin-top: 4px;
}

#mobile-button .tob-bar {
  z-index: 3;
}

#mobile-button.open .top-bar {
  transform: rotate(45deg);
  transform-origin: 10% 10%;
}

#mobile-button .mid-bar {
  z-index: 2;
}

#mobile-button.open .mid-bar {
  opacity: 0;
}

#mobile-button .bot-bar {
  z-index: 1;
}

#mobile-button.open .bot-bar {
  transform: rotate(-45deg);
  transform-origin: 10% 90%;
}

JavaScript

jQuery(document).ready(function($) {

  dhMobileMenu({
    from: 'left',
    width: '260px'
  });

  function dhMobileMenu(options) {

    var from = options.from,
      width = options.width;

    $('#mobile-button').css({
      'right': (from == 'right' ? '0px' : ''),
      'left': (from == 'left' ? '0px' : '')
    })

    $('#mobile-button').add('#mobile-shadow').on('click', function(e) {
      $('#mobile-button').toggleClass('open');
      $('#mobile-shadow').fadeToggle('open');
      $('#mobile-menu').toggleClass('open open-' + from);
      $('body').add('html').toggleClass('open');
    });
  }
});