I have a menu that is visible on desktop but hidden on mobile viewports. It becomes visible on mobile through a hamburger toggle.
My problem is that once the mobile toggle has been activated once on a mobile viewport, the menu is no longer visible when the window is resized to desktop proportions.
The only way to get it back is to reload the page. This is not something I want to be the case. I would like it to revert on resize.
(This may seem unimportant or contrived but consider the user turning their mobile screen sideways: they would loose the non-mobile menu.)
I have attached a minimal working example via JS Fiddle.
I would be very grateful for any advice. I am not very good with JS/jQ so I appreciate this may be a very rookie problem.
Thank you.
$(document).ready(function() {
$('#toggle').click(function() {
$('.hide').slideToggle('slow').addClass("show");
return false;
});
});
.block {
background-color: lightblue;
border: 2px solid blue;
}
.menu {
padding: 2em;
background-color: yellow;
}
/*Toggle controls*/
@media only screen and (max-width: 768px) {
.hide {
display: none;
}
.show {
display: visible;
}
}
/*Hide hamburger icon on desktop*/
@media only screen and (min-width: 768px) {
.hamburger {
display: none;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<div id="toggle" class="hamburger block">
☰
</div>
<div class="site title block">
Site Title
</div>
<div class="menu hide">
Placeholder for menu
</div>
</header>