What I've tried:
I'm almost expecting this to be flagged as a duplicate, but the first ten possible duplicate solutions did not work for me. I've tried unbind()
, event.stopPropagation()
and event.stopImmediatePropagation()
(however, my understanding of the function isn't the best), but my .click()
function fires three times in the overlayClicked()
function.
What I have:
I have three <div>
s that make up the body so far: the navigation drawer, the menu handle, and the overlay.
The handle is place relatively to the the drawer so that when the drawer opens and closes, it moves with it. Only when the handle is clicked, using a <label>
with a hidden <input type="checkbox">
. When the handle is clicked, (and this part works fine) it calls handleClicked(cb)
which then either opens or closes the drawer, and sets the third <div>
, the overlay, from display: none
to display: block
and fades it in.
The part that doesn't work is I also have another function, overlayClicked()
, that obviously, when the overlay is clicked, I want it to click the checkbox from above, close the drawer, and again hide the overlay. (Hopefully this all makes sense so far).
function handleClicked(cb){
if (cb.checked){
$(".navbar").css("left", "0px");
$(".overlay").fadeToggle(500);
}else{
$(".navbar").css("left", "-202.77px");
$(".overlay").fadeToggle(500);
}
console.log("handle");
}
function overlayClicked(){
$("#handle").click();
}
:root {
--light-green-rgb: 142, 167, 128;
--light-green-hex: #8EA780;
--green-rgb: 91, 110, 82;
--green-hex: #5B6E52;
--dark-green-rgb: 37, 52, 29;
--dark-green-hex: #25341D;
}
.overlay {
position : fixed;
top : 0;
left : 0;
width : 100%;
height : 100%;
background : #000;
opacity : 0.6;
filter : alpha(opacity=60);
z-index : 998;
display : none;
}
.navbar {
position: fixed;
left: -202.77px;
z-index: 999;
width: 200px;
height: 100%;
background-color: rgba(91, 110, 82, 0.9);
border-right: 2.77px solid var(--dark-green-hex);
transition: left .5s;
}
label.navbar-handle {
position: relative;
width: 100%;
height: 100%;
display: flex;
float: right;
cursor: pointer;
display: block;
}
.navbar-handle-div {
position: relative;
right: -209.77px;
top: 7px;
border: 3px double var(--dark-green-hex);
background-color: var(--green-hex);
width: 45px;
height: 45px;
}
.navbar-handle-line {
margin-left: auto;
margin-right: auto;
display: block;
width: 80%;
height: 6.28%;
border: 2px solid var(--dark-green-hex);
cursor: pointer;
}
.navbar-handle-space {
margin-left: auto;
margin-right: auto;
display: block;
width: 80%;
height: 14.28%;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="overlay" onclick="overlayClicked()"></div>
<div class="navbar">
<div class="navbar-handle-div">
<label for="handle" class="navbar-handle">
<div class="navbar-handle-space"></div>
<div class="navbar-handle-line"></div>
<div class="navbar-handle-space"></div>
<div class="navbar-handle-line"></div>
<div class="navbar-handle-space"></div>
<div class="navbar-handle-line"></div>
<div class="navbar-handle-space"></div>
</label>
<input style="display: none;" id="handle" type="checkbox" onclick="handleClicked(this)"/>
</div>
</div>
Expected behavior:
Obviously, I want to click the overlay, and have it call overlayClicked()
, with it simply unchecking the checkbox, closing the drawer, and fading the overlay out.
Actual behavior:
As you can see, when handleClicked()
is called, I log "handle" to the console, and when I click on the overlay, it logs "handle" three times, closes the drawer, fades the overlay out, and then it flashes the overlay, fading it in, and then fading it out, without opening the drawer again. I'd imagine that's simply because I'm setting the left
to 0, and then setting it to hidden again, so that's not really my concern.