Search, View and Navigation

jQuery

jQuery drop down menu delay (setTimeout)

Attention, open in a new window. PDFPrintE-mail

jQuery - Widgets


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: Just make sure to have the ul.subNav be display: none; by default in your CSS.
Comments (2)
Missing Link
2 Tuesday, 13 January 2009 21:00
Ted
The link to the source files for this example isn't working. I'd like to see you example. Could you fix the linK?
link not working
1 Tuesday, 04 November 2008 18:12
Nicolás Echániz
The example link is not working.

Add your comment

Your name:
Your email:
Your website:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:

Additional Information

What is your job title?












Results