- This topic has 13 replies, 3 voices, and was last updated 4 years, 4 months ago by
Tom.
-
AuthorPosts
-
December 17, 2018 at 2:56 am #758837
Eliza
Hello lovely humans,
I wonder if anyone have any idea how to open submenu on “click/tap” of parent element. We usually use the first link from a submenu as a parent holder, so that when on desktop on click it would jump to a subpage, and not some weird #.
So what I wanted to do was to add this little thing in my JS:
jQuery('.menu-item-has-children > a').on('click',function(e){ e.preventDefault(); jQuery(this).children('.dropdown-menu-toggle').trigger('click'); });
But unfortunately it doesn’t work, because of the
closeElements
click eventgenerateOffside.close();
. I can’t deregister it as addEventListener is an anonymous function.To sum up. I don’t want to use # as a placeholder for a dropdown menu, but I would like it to act as a toggle on mobile, but I can’t because currently there is a “close offside” event glued to that ;o) Any ideas?
December 17, 2018 at 5:15 pm #759673David
StaffCustomer SupportHi there,
as you are using the slide out navigation, you could just set a different menu for the slideout. Would that be a simpler solution?
December 18, 2018 at 12:32 am #759841Eliza
Hi David!
That’s actually clever idea, but it adds some manual work to create and maintain 2 * [language_count] menus. I’m all about making work easier so even if that’s a great idea for a quick fix, it’s against my beliefs as a long term solution ;o)
If there are no other solutions, I will have to look into menu walker to use your idea, but without adding additional manual work for a client. Besides, I’m 100% sure that after few months, client won’t remember that new page must be added in multiple menus and will be blind for any messages left in WP.
Unless there is a chance to make this function not anonymous so I could unhook this event or some any other filter that I could use.
December 18, 2018 at 9:29 am #760385Tom
Lead DeveloperLead DeveloperAny reason why you don’t want to use
#
as the parent item URL? That’s by far the easiest solution, as it’s built right into GP.December 18, 2018 at 9:41 am #760399Eliza
Hi Tom, thanks for stopping by. It’s because that’s a behavior some users expect. I did used # in some distant past, but I’ve noticed that lots of people intuitively clicks parent link. So I’ve made some research then and conclude that it doesn’t hurt to make parent element clickable.
Can’t find quickly some hard data. I remember seeing stuff like that in hotjar and also found this now while googling https://www.sitepoint.com/community/t/drop-down-menu-parent-link-active-or-null/42897 (I mean, I know it’s from 2014, but still ;o))
I saw in JS when looking for a reason why my “fix” doesn’t work that # will work, but if I could, I would love to avoid it and use parent link to first item in submenu. It just works well for us.
December 18, 2018 at 5:25 pm #760706Tom
Lead DeveloperLead DeveloperNot sure I’m 100% understanding – we’ll get there π
So, you have a parent menu item. Right now, the user needs to tap the arrow next to it to reveal the sub-menu.
If they tap the actual parent menu item, it goes to the URL attached to that menu item.
Instead of that, you want the user to be able to tap the entire parent menu item?
Or did I get lost somewhere?
Let me know π
December 19, 2018 at 6:42 am #761116Eliza
If they tap the actual parent menu item, it goes to the URL attached to that menu item.
I want to reveal the sub menu instead of jump to URL. I’m using the same menu for desktop and slideout menu, and for slideout (only) I want trigger – reveal submenu – instead of – go to this link. Just like in this little snippet:
jQuery('#generate-slideout-menu .menu-item-has-children > a').on('click',function(e){ e.preventDefault(); jQuery(this).children('.dropdown-menu-toggle').trigger('click'); });
But currently this won’t work, as there is already click event attached to those links, that closes the menu which I can’t remove, because it’s an anonymous function. I’m referring to this function in offside.js
/** * Closes the slideout navigation if the link does something. * eg. Goes to an anchor link * * @since 1.7 */ var slideoutLinks = document.querySelectorAll( '.slideout-navigation ul a' ); for ( var i = 0; i < slideoutLinks.length; i++ ) { slideoutLinks[i].addEventListener( 'click', function( e ) { var url = this.getAttribute( 'href' ); if ( '#' !== url && '' !== url && ! navigator.userAgent.match( /iemobile/i ) ) { setTimeout( function() { generateOffside.close(); }, 200 ); } } ); };
December 19, 2018 at 10:31 am #761430Tom
Lead DeveloperLead DeveloperAh, I understand. So you want it to behave differently whether it’s the regular menu vs the slideout menu.
Hmm, I’m not sure there’s a way around this currently without creating two separate menus. We could make it so that’s a named function in GPP 1.8. That way the eventlistener could be removed.
December 19, 2018 at 10:39 am #761442Eliza
Correct. Slideout menu will be used for small devices (probably touch) and thus it’s more intuitive for whole link to open a sub menu, but I don’t want to create separate menu with #. It is harder to maintain multiple menus.
If it’s possible to make that named function in some near future that would be awesome! The only other option I see now, is custom menu walker to strip url for parent item and replace it with #, but removing that event would be just simpler.
December 19, 2018 at 6:09 pm #761690Tom
Lead DeveloperLead DeveloperI’m sure that’s possible – I’ll update this topic once we implement a solution π
December 20, 2018 at 3:38 am #761892Eliza
<3 thank you
January 7, 2019 at 12:53 pm #775627Tom
Lead DeveloperLead DeveloperImplemented in GPP 1.8.
Here’s the code if you want to make the change now:
/** * Closes the slideout navigation if the link does something. * eg. Goes to an anchor link * * @since 1.7 */ var slideoutLinks = document.querySelectorAll( '.slideout-navigation ul a' ); var closeOffsideOnAction = function() { var url = this.getAttribute( 'href' ); if ( '#' !== url && '' !== url && ! navigator.userAgent.match( /iemobile/i ) ) { setTimeout( function() { generateOffside.close(); }, 200 ); } }; for ( var i = 0; i < slideoutLinks.length; i++ ) { slideoutLinks[i].addEventListener( 'click', closeOffsideOnAction, false ); };
January 9, 2019 at 1:13 am #776898Eliza
Very good news! thanks Tom! :o)
January 9, 2019 at 8:23 am #777373Tom
Lead DeveloperLead DeveloperYou’re welcome π
-
AuthorPosts
- You must be logged in to reply to this topic.