To create a navbar with a subnav (drop down menu for instance) with a delay before closing using jQuery (jQ) follow this code. An example can be found here.
Javascript (js):
function navbar() {
// create variables
var subNavTimer;
var open;
// to make sure that when user mouses over sub menu ul it stays open
$("ul.subNav").mouseover(function() {
$(this).show();
// lets remember what's open
open = $(this).parent();
});
// when user mouses over main item in navbar
$("li.navMainLink").mouseover(function() {
// close other nav item submenus
if (open != null) open.children("ul").hide();
// stop the timer
clearTimeout(subNavTimer);
// show this nav item's sub menu
if ($(this) != open) $(this).children("ul:hidden").show();
});
// when user's mouse leaves the navbar item
$("li.navMainLink").mouseout(function() {
// lets keep tabs of what is open
open = $(this);
// start the timer for 2 seconds until it closes.
subNavTimer = setTimeout('open.children("ul:visible").hide();', 2000);
});
}
$(document).ready(function() {
navbar();
});
HTML:
<ul id="navMain">
<li class="navMainLink">
<a class="navLink" href="#">Performances</a>
<ul class="subNav">
<li>
<a href="#">Season Subscriptions</a>
</li>
<li>
<a href="#">About the Shows</a>
</li>
</ul>
</li>
</ul>
CSS:
Your decision! Make it a vertical drop down or make it a horizontal fly out. Just make sure to have the ul.subNav be display: none; by default in your CSS.

#1 by onno on May 22, 2008 - 3:24 am
this line is wrong.
subNavTimer = setTimeout(‘open.children(“ul:visible”).hide();’, 2000);
#2 by Alex Grande on May 29, 2008 - 3:28 pm
What’s “wrong” about it?
I am open to improvements to anything on The True Tribe. Please provide with examples or ways to make anything better. Although it works perfectly and the link at the top of this post proves that. Jonah Dempcy suggested this though:
subNavTimer = setTimeout(function() {
open.children(“ul:visible”).hide();
}, 2000);
As a way to increase performance.
#3 by lowellk on June 8, 2008 - 11:28 pm
I’d refrain from adding those vars to the global namespace if possible. One way to do so would be to wrap your code like so:
(function() {
{YOUR CODE GOES HERE}
})();
#4 by Ulisses José on August 21, 2008 - 1:40 pm
Well, the iten navMain doesn’t show when I focus mouse pointer on the link. I think I have use a CSS style for configuration. Do you understand me? Can you help me? Tanks
#5 by Alex Grande on August 26, 2008 - 10:04 pm
If you are having trouble with the CSS I suggest double checking everything. I think you may have some coding specificity issues?