[CSS3] jQuery Dropdown Navigation in WordPress
Adding the Next Level
Here is where we separate this navigation menu from most; we will add another list of items under each current item so there are submenus when hovered. On the list items you want to have sub menus you will need to open a new unordered list before the parent closing list item, like so:
<!-- Title --> <!-- Meta --> <!-- Javascript --> <script src=" jquery-latest.min.js" type="text/javascript"><!--mce:2--></script> <!-- Styles --> <div id="header"> <ul> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> <li><a href="#">list item</a></li> </ul> </div>
jQuery Animation Time
Now, time for the fun part! Before we style those sub level menus we want to be able to see the content they hold and be able to make them appear on hovering the parent. To do this, we will use jQuery’s .hover() function.
$(function(){ //Hide SubLevel Menus $('#header ul li ul').hide(); //OnHover Show SubLevel Menus $('#header ul li').hover( //OnHover function(){ //Hide Other Menus $('#header ul li').not($('ul', this)).stop(); //Add the Arrow $('ul li:first-child', this).before( ' <li class="arrow">arrow</li> ' ); //Remove the Border $('ul li.arrow', this).css('border-bottom', '0'); // Show Hoved Menu $('ul', this).slideDown(); }, //OnOut function(){ // Hide Other Menus $('ul', this).slideUp(); //Remove the Arrow $('ul li.arrow', this).remove(); } ); });
This entry was posted on Monday, March 22nd, 2010 at 09:30 and is filed under Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Thank you! I might use it in my future WordPress website! 🙂
Nice tutorial. thanks
ok great tutorial , and I’ll leave comment for respect
There is a bug…
When you pass the dropdown menu item a couple of times, it continues animating…
If you move quickly 5 times over the button, it will animate 5 times… Is there a fix for it?
What is the fix for this?
Add a stop like this:
// Show Hoved Menu
jQuery(‘ul’, this).stop(true, true).slideDown();
},
//OnOut
function(){
// Hide Other Menus
jQuery(‘ul’, this).stop(true, true).slideUp();
Any reason you did not nest your tags properly? I mean going right into another LI without first opening a UL?
Also, you should probably add a stop before you animate. When you mouse in and out rapidly, you are forced to wait for animation.
@SD and @Matt
We will try to find a solution for this ASAP.
@SD I will look into it.
@Matt It has it on my local version. I must have messde up while typing this tutorial. When using the WordPress code
ok , good tut 😀
Nice tut.
Ive found that the arrow element gets in the way of the first link in the list.
Is there a way to make the arrow li smaller and less obtrusive?
Thanks.
Found a solution for the arrow blocking the first link in the list.
Just add z-index: -100; to the .arrow {} css element
Could you please help me with one error im getting with this tutorial.
Im getting a border on the left hand side of the .arrow element when the list animates up.
You could please let me know how to remove this.
My menu can be seen here: http://www.unit23skatepark.co.uk
Mouse over the team or contact links.
Thanks in advance.
Chris.
Tried the z-index fix above but it doesnt seem to do anything. Only way I found to get over the problem is to make the .arrow class more specific so that it overwrites the standard #header ul li ul li styles rather than defaulting to them.
Change the .arrow selector to #header ul li ul li.arrow. Then on the last line add padding-bottom: 0; and you shouldnt have the arrow obscuring the link.
Awesome tut … haven’t seen a simpler drop-down-jquery-tut anywhere.
Note: does not work if you use WordPress’s own “jquery.js” from the includes directory.
not working on localhost wordpress theme that is being developed 🙁
Is there any way to center the submenu, because if you have a menu item with the name of Portfolio, the submenu is not centered.
All the menus should be centered exactly. Please first try and rework your code to resemble the final code and then if that doesn’t work post your code.
Ok, the submenu is centered only if the menu item has a fixed width but if i change the width to auto then the submenu is not centered, is there a way to fix this?
The only way to horizontally align an UL element without a fixed width is to use something like this:
#nav ul {
clear: left;
float: left;
position: relative;
left: 50%;
}
#nav li {
display: block;
float: left;
position: relative;
right: 50%;
}
Thanks for a nice tut.
I had a problem with a not so smooth animation after hovering the menu(due to the arrow being removed)
(I used other styling with all images maybe thats why?)
If you are having a similar problem the fix is to put the ‘remove arrow’ in front ‘add arrow’.
Does the same job:
//remove the Arrow
jQuery(‘ul li.arrow’, this).remove();
//Add the Arrow
jQuery(‘ul li:first-child’, this).before(
‘arrow’
This post was extremely nicely written, and it also contains numerous helpful facts. I appreciated your professional way of writing the publish. Thanks, you have made it easy for me to understand.
Can you give me more examples? I really want to create a drop down menu for my website! Thanks for your sharing and i’m looking forward to your more attractive content!
Is there a fix for the way this menu pushes the whole page down on hover. I tried playing with z-index with no success.
Very nice code snippet. It works great!
Hi There!,
this is my first ever post/comment/question regarding a tutorial.
First off, thank you so much for sharing you knowledge with everyone. (it’s people like you that make it possible for people like me to design websites)
Question:
When I view this drop down in firefox, the border and shadow don’t show. I haven’t tried IE yet.
is there a fix?
peace!
mark
Hello Cody.
first , i�d like to thank you for a big help that you gave me with this menu.
but i still have some problems with render in the internet explorer browser .
take a look at the link: http://www.assadballarinybrito.adv.br/teste/index.html
and if you have a solution for this… because in another browser it’s working very well…
Thanks a lot!
put jQuery .stop function in your animation not to run it again and again!!
How to download it?