简体   繁体   English

jQuery菜单徘徊延迟

[英]jQuery menu hover off delay

I'm using the below jQuery/javascript to control the hover functionality of a menu. 我正在使用下面的jQuery / javascript来控制菜单的悬停功能。 When an "item-wrapper" item is hovered over, it display a tooltip submenu. 当“项目包装”项目悬停时,它会显示工具提示子菜单。

I wish to add two pieces of functionality to this code: 我希望为此代码添加两项功能:

1) For the Tooltip to only appear after 500milliseconds of hovering over a menu item 1)仅在500毫秒的鼠标悬停在菜单项上后才显示工具提示

2) For the user to be able to move off the tooltip and have it stay visible for about 1 second (thus giving them the option to move back over it before it disappears) 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();
});

All help greatly appreciated 所有帮助非常感谢

You can use setTimeout to delay the call. 您可以使用setTimeout来延迟调用。 The tricky part is making sure to clear the timeouts correctly if the user re-hovers over the element, or hovers over a different one. 棘手的部分是确保在用户重新悬停在元素上方时正确地清除超时,或者将鼠标悬停在另一个元素上。

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);
});

You may want to look into the hoverIntent plugin. 您可能想要查看hoverIntent插件。 Here's the short description: 这是简短的描述:

instead of immediately calling the onMouseOver function, it waits until the user's mouse slows down enough before making the call 而不是立即调用onMouseOver函数,它等待用户的鼠标在进行调用之前放慢速度

You can add the 500ms delay before showing the tooltip, but it's not necessary if you're just trying to prevent accidental mouse over. 您可以在显示工具提示之前添加500毫秒的延迟,但如果您只是想防止意外鼠标悬停,则没有必要。 Here's how you may implement it. 这是你如何实现它。

$(".item-wrapper").hoverIntent({
 over: function() { $('#' + $(this).attr("rel") + '-tip').delay(500).fadeIn("fast").show(); },
 timeout: 1000, // number = milliseconds delay before onMouseOut
 out: function() { $('#' + $(this).attr("rel") + '-tip').hide(); }
});

Add .delay(500) for the delay before .fadiIn, 在.fadiIn之前添加.delay(500)延迟

Add another delay at the start of the mouseleave function. 在mouseleave函数开始时添加另一个延迟。

How about use jquery delay for the mouseleave, so change it to: 如何为mouseleave使用jquery延迟,所以将其更改为:

function() {//hide tooltip when the mouse moves off of the element
$('#' + $(this).attr("rel") + '-tip').delay(500).hide();
});

to delay the hover event is a little more complicated, you need to keep a timer. 延迟悬停事件有点复杂,你需要保留一个计时器。 Something like this: 像这样的东西:

$(function() {
var timer;
$(".item-wrapper").hover(function() {
    if (timer)
    {
       clearTimeout(timer);
       timer=null;
    }
    timer = setTimeout(function() {
       $('#' + $(this).attr("rel") + '-tip').fadeIn("fast").show()
    },500);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM