enter image description hereI am using a hamburger menu which toggles class with jQuery and fades in/out a hidden menu using Javascript all in the same click function. It is working except that the icon has to be clicked twice before the menu fades in initially, not even double clicking gets it to work at the same time as the icon toggles class, after that it is fine and works as expected, just the delay on first opening page, Or is there a way to do it better with jQuery, I did try a couple of ways but couldn't get it. The code is below:
// In javascript
$( document ).ready(function() {
$(".hamburger").click(function() {
$("#primary_nav").toggleClass("is-active");
}
});
// In CSS
#primary_nav {
opacity: 0;
pointer-events: none;
transition: opacity 300ms;
}
#primary_nav.is-active {
opacity: 1;
pointer-events: auto;
}
Its all inside an init() function so Im not sure if I need the document ready. (I am using Sage 9 for Wordpress) Any tips welcome. Thanks
Update I scrapped the javascript fades to use the jQuery toggle and css as in below answer, so its like that at the moment. The original Javascript target the js-btn on click. Wordpress generates extra classes menu-main-container etc as in image below. Just need this little bit to finish the nav. Thanks
-
Some HTML for context would be great, a minimal reproducible example would be even better.Jon P– Jon P2018年02月13日 00:12:01 +00:00Commented Feb 13, 2018 at 0:12
-
Added HTML, Thanksminimallinux– minimallinux2018年02月13日 09:08:56 +00:00Commented Feb 13, 2018 at 9:08
-
That's a start, but can you change it to the HTML that is rendered to the page, hit "View Source" on the page to get the HTML that is rendered by Wordpress.Jon P– Jon P2018年02月13日 21:52:29 +00:00Commented Feb 13, 2018 at 21:52
1 Answer 1
You asked for another way, so that's what I'm providing. I don't like doing simple transitions like this in JS - I find them easier and more performant in CSS. So here's my suggestion:
// In javascript
$( document ).ready(function() {
$(".hamburger").click(function() {
$("#primary_nav").toggleClass("is-active");
}
});
// In CSS
#primary_nav {
opacity: 0;
pointer-events: none;
transition: opacity 300ms;
}
#primary_nav.is-active {
opacity: 1;
pointer-events: auto;
}
@media (min-width: 920px){
#primary_nav {
opacity: 1;
pointer-events: auto;
}
}