Hi all,
Beginner here; I practicing working on on a responsive nav that toggles between the hamburger icon and menu on small devices and displays a regular menu once the screen width exceeds 768px (using jQuery). It works overall; however, after I click the hamburger icon and the mobile menu displays, if I expand the screen, the mobile menu is still displayed. I would like it to disappear.
Full code is https://codepen.io/srhbishop/pen/PveEGz, but here are excerpts:
HTML:
<div id="hamburger">
<i class="fas fa-bars"></i>
</div>
</div>
<div id="mobile-menu">
<div id="mobile-menu-inner">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<div id="desktop-menu">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
CSS excerpt:
@media only screen and (min-width: 768px) {
#desktop-menu {
display: flex;
background-color: lightgray;
}
#hamburger {
display: none;
}
#mobile-menu {
display: none;
}
JS code:
$(document).ready(function() {
$("#hamburger").click(function() {
$("#mobile-menu").slideToggle(1000);
});
if(screen.width>768) {
$('#mobile-menu').hide();
}
});