javascript - jQuery 菜单悬停关闭延迟

标签 javascript jquery menu tooltip

我正在使用下面的 jQuery/javascript 来控制菜单的悬停功能。当“item-wrapper”项目悬停时,它会显示一个工具提示子菜单。

我想在此代码中添加两个功能:

1) 工具提示仅在悬停在菜单项上 500 毫秒后出现

2) 让用户能够离开工具提示并让它保持可见约 1 秒(从而让他们可以选择在它消失之前移回它上面)

$(".item-wrapper").hover(function() {
    $('#' + $(this).attr("rel") + '-tip').fadeIn("fast").show(); //add 'show()'' for IE
},
function() {//hide tooltip when the mouse moves off of the element
$('#' + $(this).attr("rel") + '-tip').hide();
});

非常感谢所有帮助

最佳答案

您可以使用setTimeout 来延迟调用。棘手的部分是确保在用户将鼠标重新悬停在元素上或悬停在其他元素上时正确清除超时。

var timeouts = {};
$(".item-wrapper").hover(function() {
    var rel = $(this).attr("rel");
    var el = $('#' + rel + '-tip');
    if (timeouts[rel]) clearTimeout(timeouts[rel]);
    timeouts[rel] = setTimeout(function () { el.fadeIn("fast").show(); }, 500);
},
function() {
    var rel = $(this).attr("rel");
    var el = $('#' + rel + '-tip');
    if (timeouts[rel]) clearTimeout(timeouts[rel]);
    timeouts[rel] = setTimeout(function () { el.hide() }, 1000);
});

关于javascript - jQuery 菜单悬停关闭延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5546406/

相关文章:

javascript - 使用 Jquery Filter 作为过滤页面上链接的方法

javascript - jQuery,如何制作同步动画?

javascript - 在全屏模式下加载页面

jquery - jQuery 鼠标悬停问题以获得更好看的 css 菜单

android - 选项菜单显示缓慢

javascript - 如何使用键多个值过滤数组中的数组?

javascript 正则表达式 trim 到 'space' AND 'dash' 或匹配

javascript - 安全的 html 表单变量

javascript - 在传递参数时在 SSRS 中的新窗口中打开 URL

html - 在 .active 时更改 first child::before 样式