I’m attempting to make a menu toggle for the Features and Company sections of my menu. It should:
Toggle any active menus to hide them
On a second click of the header link or clicking away from the menu, it should hide the active menu again
What I’m finding at the moment is I’ve covered the first point but cannot find a way of implementing the second without affecting what I’ve applied already.
Bind a click to the document that hides all menus;
Bind a click to the header that:
checks to see if it’s associated menu is currently shown.
Hide all menus (hey look, i’ve typed that twice. Smells like a function.)
If the check from bullet 1 was negative, show the associated menu and stop propagation.
(There may be additionals needed depending on the complexity or desired actions of your menu items. So far i just see a single layer of menu with clickable links that presumably send the user to a new page.)
Thank you for the tips!
I’ve now applied some checks that only show the associated menu and close all other menus via a function.
I now have a couple of minor issues I would like some advice on please:
When opening/closing the “Features” and “Company” menus, there is sometimes a slight pause when the show or hide occurs, particularly if you open/close quite quickly
Within the mobile view, if you open the navigation and then change to desktop or tablet view, the “show” class remains in the “nav” element, but also when changing back to the mobile view, the menu is already open in this scenario.
So if you click twice in less than 0.3s, you’re tripping into the animation sequence.
Well nothing is bound to a resizing of the window to cause it to remove a class from an element. Is that something you’re trying to do? (I wouldnt… the user opened the menu, why would you want to close it just because they turned their device sideways to see it better?)