I have the following problem: When I click on the label-"button" (hamburger menu) I have to double to click to show the menu the second time around.
I think is a conflict beween CSS and jQuery.
The intended code behavior is the following: When you click #NavDark I want to hide .MainMenu and #NavDark itself, when you click the label I want to show them both.
$("#NavDark").click(function() {
$(".MainMenu").hide();
$("#NavDark").hide();
});
$("#toggle").click(function() {
$(".MainMenu,#NavDark").show();
});
.ButtonMenu label:checked~.MainMenu {
display: block !important;
}
.ButtonMenu #toggle {
display: none;
}
.ButtonMenu label {
font-size: 40px;
color: #000;
cursor: pointer;
z-index: 8;
display: inline-block !important;
}
.MainMenu {
position: fixed;
top: 0;
width: 320px;
background-color: #222;
color: #fff;
transition: all .4s ease;
overflow-y: auto;
height: 100vh;
box-shadow: 0 0 2px #000;
z-index: 8;
opacity: 0;
right: -320px;
}
#toggle:checked~.MainMenu {
opacity: 1;
transform: translateX(-100%);
}
#NavDark {
width: 100%;
right: 0;
top: 0;
height: 100vh;
position: fixed;
background-color: rgba(0, 0, 0, .5);
opacity: 0;
z-index: -1;
}
#toggle:checked~#NavDark {
z-index: 100;
opacity: 1;
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ButtonMenu">
<input id='toggle' type='checkbox' />
<label for='toggle'>
<img src="/img/image 8.png" alt="hamburger icon">
</label>
<div id="NavDark">
</div>
<div class='MainMenu'>
{{ range .Site.Menus.main }}
<a href="{{ .URL }}" class="navbar-item" title="{{ .Name }}"> {{ .Name }}</a> {{ end }}
</div>
</div>
I need help on how to make the label only needing one click each time to show the menu.
3
Answers
It seems like the issue might be related to the order of operations in your JavaScript. The click event for #toggle is showing both .MainMenu and #NavDark, but the click event for #NavDark is only hiding them.
To solve this issue, you can modify your JavaScript to toggle the visibility of both elements when #toggle is clicked.
updated js
This is indeed a CSS-jQuery compatibility issiue. In your CSS, visibility of
.MainMenu
and#NavDark
is controlled by wether the checkbox is checked:In your JS, however, visibility of the two elements is controlled (internally) via the
style=""
display value. Now the following happens:display:block
, but are moved offscreen by the CSS.#toggle
runs, but because the style attribute already (implicitly) isblock
, nothing happens.#NavDark
to close the menu. The JS handler of#NavDark
runs, and sets the inline style value todisplay:hidden
, so they hide. The ckeckbox hasn’t been altered by either the click nor JS code, so it remains checked..MainMenu
and#NavDark
to be ofscreen. the JS handler runs, and removesdisplay:none
, but the two elements are offscreen and so don’t display.Additionally, your
z-index
values appear to be off, which means that.MainMenu
displays behind#NavDark
and is unclickable.As for a fix, you could alter the JS handler so it additionally unchecks the checkbox:
Or you could ditch the checkbox entirely and have the visibility be controlled by the style attribute:
:checked
to translare a single element#menu-wrapper
pointer-events
to remove preserve or pass-through clicks and other pointer events