Burger Menu - Sliding Left
by davidxmartins
HTML
<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>
<div id="menu" class="menu-icon" onclick="changeIcon(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<h3>
Verion .01
</h3>
<h1>
Burger menu sliding left over content
</h1>
<script>
/*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');
}
</script>
CSS
.overlay {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
right: 0;
background-color: black;
overflow-x: hidden;
transition: 0.5s;
}
.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 */
.menu-icon {
display: inline-block;
cursor: pointer;
position: absolute;
right: 50px;
top: 20px;
z-index: 5;
}
.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 {
display: flex;
align-items: center;
justify-content: center;
-webkit-align-items: center;
height: 100%;
font-size: clamp(25px, 2vw, 2vw);
color: #aaa;
font-family: sans-serif;
}
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');
}