I've created a simple toggle menu in jQuery, which is capable of specifying a breakpoint, but I'm afraid my code is too complicated. How can I make it simpler?
I also have a question about performance of code on window resize. I want to invoke a function only once if the window's width is smaller than a given breakpoint. Should I use some boolean variable, or is there a better solution?
Here's a pen.
$(document).ready(function() {
var $menuBtn = $('[href="#menu"]'),
$nav = $('#menu'),
$window = $(window),
breakpoint = 800;
function changeMenuStyle() {
if ($window.width() < breakpoint) {
$menuBtn.show();
$nav.addClass('toggled').hide();
} else {
$menuBtn.hide();
$nav.removeClass('toggled').show();
}
}
changeMenuStyle();
$window.on('resize',function() {
changeMenuStyle();
});
$menuBtn.on('click',function(e) {
e.preventDefault();
$nav.slideToggle();
});
});
* {
box-sizing: border-box;
}
body {
background: #485373;
font-family: "Lato", sans-serif;
font-weight: 300;
}
.container {
max-width: 940px;
margin: 0 auto;
}
.menu-link {
display: block;
color: #fff;
background: #202332;
width: 100%;
padding: 20px;
text-decoration: none;
}
.menu-link i {
display: inline-block;
margin: 0 10px;
}
.menu {
overflow: hidden;
}
.menu i {
display: block;
font-size: 25px;
padding: 10px 0;
}
.menu ul {
width: 100%;
}
.menu li {
width: 20%;
float: left;
text-align: center;
}
.menu a {
display: block;
padding: 20px;
width: 100%;
background: #323A51;
color: #fff;
text-decoration: none;
border-bottom: 2px transparent solid;
}
.menu a:hover {
background: #202332;
border-bottom: 2px #fff solid;
}
.menu.toggled i {
display: inline-block;
margin-right: 10px;
padding: 0;
font-size: 15px;
}
.menu.toggled li {
width: 100%;
}
.menu.toggled a {
padding: 10px;
}
main {
padding: 50px;
color: #fff;
}
main h1 {
font-size: 30px;
font-weight: 400;
line-height: 40px;
margin: 10px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<header>
<a href="#menu" class="menu-link"><i class="fa fa-bars"></i>Menu</a>
<nav id="menu" class="menu" role="navigation">
<ul>
<li><a href="#"><i class="fa fa-home"></i>Home</a></li>
<li><a href="#"><i class="fa fa-info"></i>About</a></li>
<li><a href="#"><i class="fa fa-book"></i>Services</a></li>
<li><a href="#"><i class="fa fa-users"></i>Team</a></li>
<li><a href="#"><i class="fa fa-envelope"></i>Contact</a></li>
</ul>
</nav>
<!-- /#menu -->
</header>
<main>
<h1>Toggle menu</h1>
<p>Vestibulum tempus at ipsum a finibus. Mauris pharetra eleifend odio quis lacinia. Pellentesque ullamcorper, dolor vel congue tincidunt, mi nisl suscipit metus, nec bibendum elit ligula vitae leo. Integer volutpat semper iaculis. Mauris fermentum, enim ac finibus scelerisque, magna nibh volutpat dolor, vitae sodales mi augue molestie augue. Aliquam ultricies eget mi vel viverra. Morbi id tortor purus. Vivamus sed pretium justo. Fusce porttitor eu neque quis volutpat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ultrices metus a aliquet tincidunt. Ut pellentesque dui nec ipsum fringilla ultricies. </p>
</main>
</div>
<!-- /.container -->