我試圖添加一個延遲,在我的下拉菜單上工作,如果人員misstakly 移動鼠標的菜單之外。我已經嘗試了很多解決方案,但在這裏找不到其中的工作縫。jquery懸停meny延遲隱藏mouseleave,但停止隱藏,如果鼠標進入時間限制
編輯: 這裏是鏈接到jsfiddle for a complite example
編輯:5
$(function() {
var slideDownTime = 400;
var firstRun = true;
var $menu = $('#menu'),
$submenus = $menu.find('ul'),
$items = $menu.find('li'),
hide = function ($el, $instantHide) {
if (!firstRun) {
if (!$instantHide) {
// force the menu item to show.
$el.css({ visibility: "visible", display: "block" });
var hideMenuId = setTimeout(function() {
$el.hide().css('visibility', 'hidden');
}, 5000);
$el.data('hideMenuId', hideMenuId);
} else {
$el.hide().css('visibility', 'hidden');
}
} else {
$el.hide().css('visibility', 'hidden');
}
},
show = function ($el) {
clearTimeout($el.data('hideMenuId'));
$el.css('visibility', 'visible').stop(1, 1).slideDown(slideDownTime);
hide($el.parent().siblings().find("ul"), true); // <-- this line hides the other submenus if hovered.
var singleLevel = $el.parent().find("ul > li:not(:has(ul)) > a");
singleLevel.hover(function() {
hide($(this).closest("ul").children('li').not(this).find("ul"), true);
}, function() {
});
firstRun = false;
};
$menu.focusout(hide($submenus));
$items.hover(function() {
show($(this).children('ul'));
}, function() {
hide($(this).children('ul'), false);
});
// find the root elemement level with no children.
$menu.find("> li a").not("ul li ul a").not("li:has(ul) > a").hover(function() {
//hide the unwantend menu items.
hide($(this).closest("ul").children('li').not(this).find("ul"), true);
}, function() {
});
});
確定這就是我結束了。 但這不能用較少的代碼?
編輯:6 jsfiddle鏈接現在更新爲最新版本。
你可以把一個例子,在你的jsfiddle「懸停」方法「hoverIntent」?還有定時器代碼... – elclanrs 2012-02-25 23:21:16
什麼是定時器代碼?我能想到的唯一的計時器代碼是1000毫秒的setTimeout。 – 2012-02-26 00:35:03