我有三个级别的导航菜单:主菜单始终可见,子菜单和子菜单。
子菜单和子子菜单应仅在悬停时出现。我正在使用带有此配置的hoverint jquery插件:
jQuery(function() {
var mainmenu_cfg = {
interval: 200,
over: openSubmenu,
sensitivity: 100,
timeout: 500,
out: closeSubmenu
};
function openSubmenu() {
$("ul#submenu").css('visibility', 'visible');
}
function closeSubmenu() {
$("ul#submenu").css('visibility', 'hidden');
}
var submenu_cfg = {
over: openSubsubmenu,
sensitivity: 100,
timeout: 500,
out: closeSubsubmenu
}
function openSubsubmenu() {
console.info($(this).next());
$(this).next().css('visibility', 'visible');
$("ul#submenu").css('visibility', 'visible');
}
function closeSubsubmenu() {
$(this).css('visibility', 'hidden');
$(this).next().css('visibility', 'hidden');
}
$("li.servizi a").hoverIntent(mainmenu_cfg);
$("#submenu > li a").hoverIntent(submenu_cfg);
});
当我将鼠标悬停在主菜单中的链接上时,第一个子菜单会正确显示,但是当我将鼠标移到子菜单上时,这会在一段时间后消失(我想是因为调用了“closeSubmenu”函数)。我想,子菜单会发生同样的事情,但我仍然被困在这里,所以......
如何保持子菜单打开? 还有一件事:mainmenu是垂直的,第一个子菜单是水平的,所以我需要在“a”元素上调用hoverIntent函数而不是“li”,否则它会激活整个“li”,这实际上是更长的时间。
编辑:
这是一个演示: http://jsfiddle.net/dtBzE/
编辑2: 我差点解决了添加很多条件的问题。它并不完美,需要优化,但它有效:
jQuery(function() {
var mainmenu_cfg = {
interval: 200,
over: openSubmenu,
sensitivity: 100,
timeout: 500,
out: closeSubmenu
}
var submenu_cfg = {
over: openSubsubmenu,
sensitivity: 100,
timeout: 200,
out: closeSubsubmenu
}
var subsubmenu_cfg = {
over: onSubsubmenu,
sensitivity: 100,
timeout: 500,
out: offSubsubmenu
}
var hover_submenu = false;
var hover_subsubmenu = false;
function openSubmenu() {
$("ul#submenu").css('visibility', 'visible');
}
function closeSubmenu() {
if (hover_submenu || hover_subsubmenu) {
$("ul#submenu").css('visibility', 'visible');
hover_submenu = false;
hover_subsubmenu = false;
}
else {
console.info(hover_submenu);
console.info('close submenu');
$("ul#submenu").css('visibility', 'hidden');
}
}
function openSubsubmenu() {
hover_submenu = true;
$(this).next().css('visibility', 'visible');
}
function closeSubsubmenu() {
if (!hover_subsubmenu)
$(this).next().css('visibility', 'hidden');
if (hover_submenu || hover_subsubmenu)
{
$("ul#submenu").css('visibility', 'visible');
hover_submenu = false;
hover_subsubmenu = false;
}
else if ((hover_submenu == true) && (hover_subsubmenu == false)) {
$(this).next().css('visibility', 'hidden');
$("ul#submenu").css('visibility', 'visible');
hover_submenu = false;
} else {
$("ul#submenu").css('visibility', 'hidden');
}
}
function onSubsubmenu() {
hover_subsubmenu = true;
$(this).parent().parent().css('visibility', 'visible');
}
function offSubsubmenu() {
$(this).parent().parent().css('visibility', 'hidden');
}
$("li.servizi a").hoverIntent(mainmenu_cfg);
$("#submenu > li a").hoverIntent(submenu_cfg);
$("ul.subsubmenu > li a").hoverIntent(subsubmenu_cfg);
});