Simple drop down menu with jquery
28Jul2008 Filed under: dev Author: DanHere is a real life working example of doing a nested dropdown menu with jQuery. Interesting? Yes of course it is…
Html:
-
<ul id="nav">
-
<li><a href="#">menu item 1</a>
-
<ul>
-
<li><a href="#">sub menu item 1</a>
-
<ul>
-
<li><a href="#">sub sub menu item 1</a></li>
-
<li><a href="#">sub sub menu item 2</a></li>
-
<li><a href="#">sub sub menu item 3</a></li>
-
<li><a href="#">sub sub menu item 4</a></li>
-
</ul>
-
</li>
-
</ul>
-
</li>
-
</ul>
Javascript (jquery):
-
$(document).ready(function() {
-
$(‘ul#nav > li’).hover(function() {
-
$(‘ul:first’, this).show();
-
},
-
function() {
-
$(‘ul:first’, this).hide();
-
});
-
-
$(‘ul#nav li li’).hover(function() {
-
$(‘ul:first’, this).each(function() {
-
$(this).css(‘top’, $(this).parent().position().top );
-
$(this).css(‘left’, $(this).parent().position().left + $(this).parent().width() );
-
$(this).show();
-
});
-
},
-
function() {
-
$(‘ul:first’, this).hide();
-
});
-
});
Enjoy, see my working full example at the labs…
magnify
July 29th, 2008 at 9:59 am
Nice!
Jess
August 13th, 2008 at 11:05 pm
Bless you! Just what I was looking for and it works perfectly!
Dan
August 14th, 2008 at 4:09 pm
I’m glad it was helpful – Jess
Laurie
October 18th, 2008 at 3:06 pm
Looks great, but how do I get it to work right in IE.
Dan
October 19th, 2008 at 12:00 am
It works in IE7 and my guess is as well in IE6… What issues have you got Laurie?
bl
December 25th, 2008 at 3:43 am
Ie6 issue: submenu appears to the right of text link
and not below —-
Estevão
February 4th, 2009 at 9:42 pm
Great, works perfectly!
Thanks!
m@gmail.com
February 11th, 2009 at 5:42 pm
IE7 : submenu appears to the right of the text link, not below. anyone have a solution for this? i tried changing the positioning of the submenu in the js to:
$(this).css(’top’, $(this).parent().position().top + $(this).parent().height() ); but it didnt work at all.
?
Dan
February 11th, 2009 at 6:12 pm
Damn how have I missed that, it works at the website I created this for, will look into where I have done something wrong in the example…
Will post an update…
Lloyd
February 17th, 2009 at 4:25 am
Great work! However, I also have this issue in IE7. Any workaround for it?
Lloyd
February 17th, 2009 at 4:50 am
I just figured this out, here’s my workaround:
somewhere in the hover event…
$(’ul#nav > li’).hover(function() {
// IE workaround
if($.browser.msie) {
$(’ul’, this).css(’top’,$(this).position().top + $(this).height());
$(’ul’, this).css(’left’,$(this).position().left);
}
$(’ul:first’, this).show();
}
Thanks again for the brilliant code!
Lloyd
February 17th, 2009 at 4:52 am
Workaround:
Dan
February 17th, 2009 at 9:25 am
Thanks Lloyd, I will update the plugin when I have some spare time…
Marcin
May 11th, 2009 at 12:37 am
Hell, yeah. My client said “Marcin, is it possible to do a fancy drop down menu by tomorrow?”
So I searched a web and told him: “Sure”
Thanks buddy.
Fretsnkeys
June 1st, 2009 at 7:01 am
Hey! Thanks for the Nifty tip. I like it.
Marc Schutzbank
August 4th, 2009 at 6:21 am
Hi Dan. I really like your code and I’m new to the design game. I was having the same trouble as Lloyd and could not figure out where to put the fix. Where exactly does that go? I’d really appreciate the help.
Cheers,
Marc
George Garchagudashvili
August 25th, 2009 at 2:08 pm
Hey thanks for this, it works great
Roald
October 22nd, 2009 at 12:11 pm
Thanks for this jQuery dropdown menu! The ie workaround seems to do the trick!
jQuery Links | K! Blog
December 5th, 2009 at 2:32 pm
[...] 5 – Simple drop down menu with jquery http://anotherdan.com/9/simple-drop-down-menu-with-jquery [...]