Using JQuery, I want to be able to collapse my navigation by clicking elsewhere on the page (anything that isnt the burger). I think I understand the logic, but just fail to make it work.
Please see the following JFiddle. https://jsfiddle.net/ywkxyjx3/33/
This is the logic I have so far:
$(".burger").click(function() {
$(".nav").toggleClass("show", 900); // toggles on .show css
$(".burger").toggleClass("change"); // toggles on .change css for hamburger animation
});
$('body').click(function(event){ //onBodyclick
if ($(!event.target).closest('.navCont').length) { //if click isnt
navCont or any child elements then..
return; //do nothing
} else {
// do something when not targeted
}
});
After some research I have found similar questions but I am failing to apply it to my own, self built navigation.
How to hide my collapse bootstrap 3 navbar with click on body when, collapse is visible?
How to close an open collapsed navbar when clicking outside of the navbar element in Bootstrap 3?