- This topic has 26 replies, 2 voices, and was last updated 1 year, 3 months ago by David.
-
AuthorPosts
-
April 22, 2018 at 1:57 am #557026Nick
Hi,
Is there an easy way to make the submenu only ever have one of its drop downs open at a time?
I’m thinking of some javascript to remove any .toggled-on class that is present on any elements in an onclick event for a submenu element that has children.
Thank youApril 22, 2018 at 9:02 am #557278LeoStaffCustomer SupportHi there,
Not quite sure if I understand.
Do you have an example to show?
Let me know π
April 22, 2018 at 7:44 pm #557562NickHere you go: http://jsfiddle.net/rsewsg4t/2/
Notice how when a menu item is clicked when another menu item is open, the first open menu item closes and the clicked one expands.April 22, 2018 at 9:16 pm #557594LeoStaffCustomer SupportIsn’t that same as the click option?
https://docs.generatepress.com/article/navigation-dropdown/#click-%E2%80%93-menu-itemOr I’m missing something?
April 23, 2018 at 7:29 pm #558579NickNo, not on the example I have, which I linked when I started this thread for only admins to see.
The other expanded menu’s don’t close when a new one is opened. They stay open.
Should this not be happening normally with a Generatepress theme?April 23, 2018 at 11:22 pm #558658TomLead DeveloperLead DeveloperAh, the top level items close when another opens, but not the sub-menus. I’ll get that added into the theme ASAP.
I’ll update you when I have something working π
April 23, 2018 at 11:27 pm #558662NickNice one Tom, Thank you! π
March 12, 2019 at 8:50 am #836837BobHi GP peeps,
Just came across this and it could come in very useful.
Was a fix ever found or something implemented for the above please? If not is there a workaround or one coming in the next version possibly?
Thanks and regards,
Bob
March 12, 2019 at 3:40 pm #837138TomLead DeveloperLead DeveloperHi there,
Not super easy, but can be done with some javascript.
If you don’t already have a custom javascript file, you can:
1. Create a new Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
2. Set the hook towp_footer
, and set the Display Rules to your Entire Site.
3. Add this as the hook content:<script> var parentElementLinks = document.querySelectorAll( '.main-navigation .menu-item-has-children > a' ); for ( var i = 0; i < parentElementLinks.length; i++ ) { parentElementLinks[i].addEventListener( 'click', function() { var _this = this; var openedSubMenus = _this.closest( 'nav' ).querySelectorAll( 'ul.toggled-on' ); if ( openedSubMenus && ! _this.closest( 'ul' ).classList.contains( 'toggled-on' ) && ! _this.closest( 'li' ).classList.contains( 'sfHover' ) ) { for ( var o = 0; o < openedSubMenus.length; o++ ) { openedSubMenus[o].classList.remove( 'toggled-on' ); openedSubMenus[o].closest( 'li' ).classList.remove( 'sfHover' ); } } }, true ); } </script>
Let me know π
March 17, 2019 at 1:02 pm #841634BenI’m having the exact same problem with my site. I want to use ‘click’ menus rather than ‘hover’ menus. I have quite a number of sub-menus and the whole thing can become a real mess unless you remember to ‘unclick’ each menu to close it before opening the next one.
I’ve tried Tom’s suggestion above, setting a wp_footer hook element and applying the display rules to the entire site, but it didn’t make any difference for me.
I’d really welcome any further suggestions on how to make the sub-menus behave properly.March 17, 2019 at 4:33 pm #841720TomLead DeveloperLead DeveloperHi there,
The click menu items should do this by default. Any chance you can link me to your site?
March 17, 2019 at 4:46 pm #841735BenThe main menu items close automatically when a different main menu item is clicked. But no sub-menus close automatically. If a main menu item has five sub-menus on it, it’s possible to click open a sub-menu, then another sub-menu, etc until you have all five of your sub-menus open at once. It’s the same problem that Nick described in this thread on April 23 last year.
Is it possible to send you a private message with a link to the site? It’s not currently public, so I’d need to give you a login too…March 18, 2019 at 9:20 am #842357TomLead DeveloperLead DeveloperI just played with this a bit, and unfortunately, it’s quite complex to target opened sub-menus that aren’t a part of the item you just clicked. I’ll play with it some more, but I’m not sure if there’s an easy solution.
March 18, 2019 at 9:32 am #842365BenThank you very much Tom!
My menu has 9 sub-menus, some of them are nested within other sub-menus. Using the ‘hover’ menu it works fine, but the Sub-menu item only links to ‘#’, so it seems more user-friendly to me to use clicks instead of hover in this case. At the moment though, with it being possible to have all those sub-menus open at the same time, it soon becomes confusing for the visitor!
I appreciate the time you’re taking to look into this π
March 23, 2019 at 9:45 am #847603TomLead DeveloperLead DeveloperJust played with this a bit. Since you’re already using jQuery on the site, try this as your JS:
<script> jQuery( document ).ready( function( $ ) { $( '.main-navigation .main-nav > ul > li > .sub-menu > li.menu-item-has-children > a' ).on( 'click', function() { var _this = $( this ), thisLi = _this.closest( 'li' ), thisSubMenu = thisLi.children( '.sub-menu' ); $( '.sub-menu li.sfHover ').not( thisLi ).removeClass( 'sfHover' ); $( '.main-nav > ul > li > .sub-menu .sub-menu.toggled-on').not( thisSubMenu ).removeClass( 'toggled-on' ); } ); } ); </script>
-
AuthorPosts
- You must be logged in to reply to this topic.