Responsive Menu using jQuery
How to Create Mobile-friendly Responsive Menu
by tjbaezid
HTML
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<div class="menu-logo"><img class="logo-img" src="#"></div>
<div style="text-align:right;margin-right:10px;">
<span> [email protected]</span>
</div>
<nav>
<span id="menu-bar"><i class="fa fa-bars" aria-hidden="true"></i></span>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">about</a></li>
<li><a href="#">service</a></li>
<li><a href="#">blog</a></li>
<li><a href="#">testimonials</a></li>
<li><a href="#">contact us</a></li>
</ul>
</nav>
CSS
body {
margin: 0;
padding: 0;
height: 100%;
}
#menu-bar {
font-size: 20px;
text-align: right;
cursor: pointer;
display: none;
}
.menu-logo {
float: left;
}
nav {
width: 100%;
text-align: center;
}
nav ul {
background-color: #A4D54C;
float: right;
font-size: 20px;
line-height: 50px;
}
nav li {
display: inline;
list-style-type: none;
}
nav a {
text-decoration: none;
padding: 10px;
color: #000;
text-transform: uppercase;
}
@media only screen and (max-width: 768px) {
#menu-bar {
display: block;
margin-right: 10px;
/* set this margin because its close to the window screen*/
}
.menu-logo {
width: 100%;
text-align: center;
}
nav {}
nav ul {
float: none;
display: none;
/* set display none for use the click fucntion to show the menu*/
background-color: #A4D54C;
/* Set margin and padding 0 for full width border bottom*/
margin: 0;
padding: 0;
}
nav li {}
nav a {
display: block;
color: #fff;
border-bottom: 1px solid #000;
margin: 0;
padding: 3px;
}
}
JavaScript
$(document).ready(function() {
$('#menu-bar').click(function() {
$("nav ul").slideToggle("slow"); /* this function show/hide the menu when you click the bar icon */
})
$('nav ul li a').click(function() {
$('nav ul ').slideToggle("slow"); /* this function hide the menu when you click on a link*/
})
});