I am trying to get a jumbo slide in navigation to close when the user clicks outside of the navigation.
I currently have a click event set on a hamburger icon which toggles a class on the navigation. This class uses CSS to reposition the navigation (As well as adds a class to the body tag). The navigation covers around 75% of the screen width. However, I would like the user to be able to close the menu when they click outside this menu and not just the hamburger icon. I currently have the following
$('.c-primary-nav-toggle').on('click', function() {
$(this).toggleClass('open'); /* used to change the style of the hamburger */
$('.off-canvas-nav').toggleClass('menu-in'); /* used to bring the nav into view */
});
$(document).on('click',function(){
$('body').hasClass('mobile-menu-open').find('.off-canvas-nav').removeClass('menu-in');
});
/* Adds class to the body when hamburger is click in order to remove scroll bar from body */
$('body').removeClass('mobile-menu-open');
$(".c-primary-nav-toggle").on('click',function() {
$('body').toggleClass('mobile-menu-open');
});
<body>
<div class="c-primary-nav-toggle"></div>
<div class="off-canvas-nav">
NAVIGATION
</div>
</body>