Burger Menu - Sliding Left

by davidxmartins

HTML

<div class="header">
<div id="menu" class="menu-icon" onclick="changeIcon(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>
</div>

<div id="myNav" class="overlay">
  <ul>
    <li> <a href="#">Home</a></li>
    <li> <a href="#">Services</a></li>
    <li> <a href="#">Products</a></li>
    <li> <a href="#">About</a></li>
    <li> <a href="#">Contact</a></li>
  </ul>
</div>

<h3>
Version .02
</h3>
<h1>
Burger menu sliding left over content. Icon inside header!
</h1>

CSS

.overlay {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  right: 0;
  background-color: black;
  overflow-x: hidden;
  transition: 0.5s;
  display: flex;
  justify-items: center;
}

.overlay ul {
  list-style: none;
  padding: 20px 7vw;
  position: relative;
  top: 0;
  width: 100%;
  text-align: left;
  margin-top: 30px;
}

.overlay li {
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.overlay a {
  display: block;
  padding: 2vh 8px;
  text-decoration: none;
  font-size: 36px;
  color: #fff;
  transition: 0.2s;
  text-transform: lowercase;
}

.overlay a:hover,
.overlay a:focus {
  color: #aaa;
}

/* Menu Button Icon */
.header {
  background: #aaa;
  height: 50px;
  position: relative;
  background: #aaa;
  top: 0;
}

.menu-icon {
  display: inline-block;
  cursor: pointer;
  position: absolute;
  right: 0;
  z-index: 999;
  padding: 5px;
}

.bar1,
.bar2,
.bar3 {
  width: 35px;
  height: 5px;
  background-color: #333;
  margin: 6px 0;
  transition: 0.4s;
}

.change .bar1 {
  transform: translate(0, 11px) rotate(-45deg);
  background-color: white;
}

.change .bar2 {
  opacity: 0;
  background-color: white;
}

.change .bar3 {
  transform: translate(0, -11px) rotate(45deg);
  background-color: white;
}




/* NOT RELEVANT - IGNORE */
html {
  height: 100%;
  font-family: sans-serif;
  font-size: clamp(25px, 2vw, 2vw);
  color: #aaa;
}

JavaScript

/*jslint browser: true*/
/*global $, jQuery*/
$('#menu').click(function() {
  if ($(this).hasClass('active')) {
    $(this).toggleClass('active');
    closeNav();

  } else {
    $(this).toggleClass('active');
    openNav();
  }
});

function changeIcon(x) {
  x.classList.toggle("change");
  let state = x.getAttribute('state');
  if (state == 'opened') {
    closeNav();
  } else {
    openNav();
  }
  // console.log(state);
}

function openNav() {
  document.getElementById("myNav").style.width = "100%";
  document.getElementById('menu').setAttribute('state', 'opened');
}

function closeNav() {
  document.getElementById("myNav").style.width = "0%";
  document.getElementById('menu').setAttribute('state', 'closed');
}