Menu dropdown on hover and all parents link
-
Is there a way to cause the parent links of the menu to drop down when hovered over and link to their pages when clicked? I understand why Customizr is set up this way (to be usable on smartphones and tablets) but for what I’m doing, I need it to work on hover.
-
No, that solution doesn’t work.
It makes the top level menu items clickable, but the menu’s won’t appear.
Can anyone suggest how to make the menu’s appear on hover, and that first one to remain clickable?
Much appreciated.
partial solution here: https://www.ads-software.com/support/topic/navigation-menu-fade-out. This makes the top level menus appear on hover.
And this made the top level menu items clickable while still having the dropdowns show up: https://www.ads-software.com/support/topic/main-menu-not-clickable
Suggestion: make a child theme to save all your changes. Only two files are necessary in your child theme directory:
- ./styles.css
- ./parts/class-header-nav_walker.php
https://managewp.com/how-to-create-a-child-theme
Problem solved.
Put this into your child theme’s style.css or into cutomizr style.css. Do not put it in Custom CSS textarea under Appearance–>Customiz’it!, as it will strip the “>” and it won’t work!
ul.nav li.dropdown:hover > ul.dropdown-menu{ display: block; } ul.nav li.dropdown > ul.dropdown-menu {top: 96%; padding-top: 10px;}
Install Real Time Find and Replace plugin. Under Tools->Real Time Find and Replace, replace
data-toggle="dropdown" data-target="#"
with a single space character. The text to be replaced should also have a single space character before it. No regex. No mods to parent theme files needed. Should work.
As a result, it might get a bit tricky when on narrow layout, because the submenus will appear and dissapear on hover. That’s the reason why the children are only displayed on parent click in the first place. It’s a trade-off. A bit better on computer, much worse on smartphone. ??
That looks easier.
I’ll play with that.
Thanks for the advice acub.
The code
ul.nav li.dropdown:hover > ul.dropdown-menu{ display: block; }
needs to have the extra margin declaration in there:
ul.nav li.dropdown:hover > ul.dropdown-menu{ display: block; margin: 0;}
The reason is that if you don’t have it then moving the mouse slowly down towards the submenu makes it collapse again as you hover over the margin. If the margin is 0 then this cannot happen.@acub Compare your site with mine and you’ll see what I mean. You need to move slowly (as many users do, especially on trackpads) to see the effect.
I thought I took care of that with the
top: 96%;
declaration. I’m not losing the .dropdown-menu on my website, in any browser, and I don’t use margin:0 on .dropdown-menu, no matter how slowly I move on those pixels. Are you losing it?
But, imho, this is a minor issue, it’s just that I don’t like the dropdown to overlap .navbar-inner, which is visible on my site, unlike on yours.
You are right in principle.
@acub
IDK what I did wrong because it’s not working for me, but I also don’t understand what you mean by “a single space character”. Also, is thisdata-toggle="dropdown" data-target="#"
all supposed to be in the Find box? Or am I supposed to find data-toggle=”dropdown” & replace with data-target=”#”?@bnock:
You actually want to strip the data-toggle=”dropdown” and data-target=”#” declarations from the page. So you need to find and replace them with a space. Those declarations are making the parent anchors not go where they’re supposed to, but instead to “#” (dead links). By removing them you keep the elements in the menu with their original links.You could replace them one by one, with two sepparate replaces, but I noticed they’re always together, in that order, so you may strip them in only one replace.
Ok now I’m real confused because I had it the way you said (both in the Find box) and it didn’t work. But I put data-toggle=”dropdown” in the Find box & data-target=”#” in the Replace box and it’s working.
Thanks
@bnock: do not replace data-toggle=”dropdown” with data-target=”#”.
data-toggle=”dropdown” – tells the browser to open the submenu on click (we don’t need that, since it’s opening on mouse-over)
data-target=”#” – replaces the link on the parent element with # (dead link, just scrolls the page to top) (we don’t want that, we want to keep the original link of the parent).
Now, the best way to make sure you are replacing both of the above with nothing (” ” = one single space character) is to:
a) disable/delete any replace you might have right now regarding this issue,
b) go in your page source and find the declarations above. They should be one after the other.
c) Copy the declarations as they are in the source,
d) paste it in the left window and
e) input a space in the right window.Right now the links on your parent elements are dead, if you replaced what you said above.
Hey all, great thread, which led me to my solution. I noticed I was having issues with mobile after pdwalker and acub’s solutions above, so I edited header-nav_walker.php to use WordPress’s built-in mobile check, and serve up one or the other:
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { $item_html = ''; parent::start_el( $item_html, $item, $depth, $args); if ( $item->is_dropdown && ( $depth === 0)) { // Enable top level links on desktop, disable on mobile if ( wp_is_mobile() ) { $item_html = str_replace( '<a' , '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"' , $item_html); $item_html = str_replace( '</a>' , ' <b class="caret"></b></a>' , $item_html); } else { $item_html = str_replace( '<a' , '<a class="dropdown-toggle"' , $item_html); $item_html = str_replace( '</a>' , ' <b class="caret"></b></a>' , $item_html); } } elseif (stristr( $item_html, 'li class="divider' )) { $item_html = preg_replace( '/<a[^>]*>.*?<\/a>/iU' , '' , $item_html); } elseif (stristr( $item_html, 'li class="nav-header' )) { $item_html = preg_replace( '/<a[^>]*>(.*)<\/a>/iU' , '$1' , $item_html); }
@imtheirwebguy (or somone other who can explain)
is your solution for the css-box or do I need to put it somewhere in the theme-file?
thanks ??
- The topic ‘Menu dropdown on hover and all parents link’ is closed to new replies.