jQuery
jQuery drop down menu delay (setTimeout)
Last Updated (Wednesday, 22 October 2008 05:28) Written by Alex Grande Thursday, 28 August 2008 05:26
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.
| < Prev |
|---|
Comments (1)
link not working
1
Tuesday, 04 November 2008 18:12
Nicolás Echániz
The example link is not working.



