I'm trying to mimic android's side menu style. here is the link to my codepen https://codepen.io/Killerbee98/pen/KqreZw. My problem is that it doesn't close when i press outside of the navigation as it does on android. Now i have done some research on how to do smth like this; first would be to use:
$('#menucontainer').click(function(event){
event.stopPropagation();
});
but this isn't a practical method and is wrong to use.
Second would be to add a click listener using :
$(document).click(function(event){
if (!$(event.target).closest('.navigation').length) {
$('.navigation').hide();
}
});
But this doesn't work with my code since my code has a click listener:
$('.menu-btn').click(function() {
console.log("button pressed");
if($('.navigation').width() === 250){
$('.navigation').width('0px');
}else{
$('.navigation').width('250px');
}
})
i tried doing this:
$('.menu-btn').click(function() {
console.log("button pressed");
if($('.navigation').width() === 250){
$('.navigation').width('0px');
$(document).click(function(event){
if (!$(event.target).closest('.navigation').length) {
$('.navigation').hide();
}
});
}else{
$('.navigation').width('250px');
}
})
it closes but doesn't open back again as it should.
how do i go about solving my issue?