Skip to content

Instantly share code, notes, and snippets.

@jreiher2003
Last active October 21, 2015 13:35
Show Gist options
  • Save jreiher2003/86dc873eb463a47fdf11 to your computer and use it in GitHub Desktop.
Save jreiher2003/86dc873eb463a47fdf11 to your computer and use it in GitHub Desktop.
mobile hamburger menu with inline javascript to open and close
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<header class="header">
<div class="header__inner">
<h1 class="header__title">
Jeff Times
</h1>
<a id="menu" class="header__menu">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M2 6h20v3H2zm0 5h20v3H2zm0 5h20v3H2z"/>
</svg>
</a>
</div>
</header>
<nav id="drawer" class="nav">
<ul class="nav__list">
<li class="nav__item"><a href="#">News</a></li>
<li class="nav__item"><a href="#">Events</a></li>
<li class="nav__item"><a href="#">Culture</a></li>
<li class="nav__item"><a href="#">Blog</a></li>
</ul>
</nav>
<script>
/*
* Open the drawer when the menu ison is clicked.
*/
var menu = document.querySelector('#menu');
var main = document.querySelector('main');
var drawer = document.querySelector('.nav');
menu.addEventListener('click', function(e) {
drawer.classList.toggle('open');
e.stopPropagation();
});
main.addEventListener('click', function() {
drawer.classList.remove('open');
});
</script>
</body>
</htmL>
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
font-family: Roboto;
font-size: 12px;
color: #424242;
}
h1, h2, h3, h4, h5 {
margin: 0;
}
header, nav, footer, section, article, div {
box-sizing: border-box;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
.header {
min-height: 56px;
text-align: left;
}
.header__inner {
width: 100%;
height: auto;
margin-left: auto;
margin-right: auto;
margin-bottom: 0;
}
.header__title {
font-size: 2em;
}
.nav {
height: 100%;
width: 100%;
/* position: absolute; *//* This trasform moves the drawer off canvas. */
-webkit-transform: translate(-300px, 0);
transform: translate(-300px, 0);/* Optionally, we animate the drawer. */
transition: transform 0.3s ease;
}
.nav.open {
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
background-color: red;
opacity: .8;
}
.nav__item {
display: list-item;
width: 100%;
text-align: center;
}
.header__menu {
display: inline-block;
}
.header__menu {
width: 32px;
fill: #211616;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment