Skip to content

Instantly share code, notes, and snippets.

@abalter
Created April 5, 2018 03:55
Show Gist options
  • Save abalter/f84c89a1a5fcaa33c9d07074adb1943e to your computer and use it in GitHub Desktop.
Save abalter/f84c89a1a5fcaa33c9d07074adb1943e to your computer and use it in GitHub Desktop.
Super simple flexbox responsive nav
<div id="nav">
<div id="menu-toggle"><button id="hamburger">MENU</button></div>
<div class="nav-items">
<a href="#home" class="active">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
</div>
$('#menu-toggle').on('click', (e) =>
{
console.log("menu-toggle");
toggleCSS('.nav-items', 'display', 'block', 'none');
});
$('.nav-items a').on('click', (e) =>
{
toggleCSS('.nav-items', 'display', 'block', 'none');
});
function toggleCSS(element, attribute, state1, state2)
{
console.log(`element: ${element} attribute: ${attribute} state1: {state1} state2: ${state2}`);
console.log($(element).css(attribute));
$(element).css(attribute) == state1 ? $(element).css(attribute, state2) : $(element).css(attribute, state1);
return $(element);
}
$(window).resize(() => {location = location; console.log($('.nav-items').css('display'))});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
body {
font-family: Arial, Helvetica, sans-serif;
}
#nav {
}
.nav-items {
width: 100%;
display: block;
}
.nav-items a {
text-align: center;
display: inline-block;
}
.nav-items a:hover {
color: red;
}
#menu-toggle {
display: none;
margin: 0 auto;
}
@media screen and (max-width: 600px) {
#nav {
text-align: center;
}
.nav-items {
display: none;
margin: 1rem auto;
}
.nav-items a {
display: block;
}
#menu-toggle {
display: block;
}
}
@media screen and (min-width: 601px) {
.nav-items {
display: inline-block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment