如何中断 Hover 的 handlerOut

发布于 2024-11-19 03:49:37 字数 378 浏览 7 评论 0原文

我有以下情况:

我有一个对象,我们称其为“按钮”。当您将鼠标悬停在“按钮”上时,它会使另一个对象“信息”向下滑动。当然,当你的鼠标离开Button时,Info就会向上滑动并消失。但是,Info 有一个链接,用户可能想要单击它。我可以延迟信息向上滑动,但到达信息后我无法停止它。

这是我正在使用的代码。

$("#button").hover(function(){
    $("#info").slideDown("fast");
}, function(){ //the handlerOut
    $("#info").delay(1000).slideUp("fast");
});

如何告诉 Button 在鼠标悬停后不要滑动信息?

I have the following situation:

I have an object, lets call it "Button". When you mouese over Button it make another object "Info" slide down. Of course, when your mouse leaves Button, Info slides up and dissapear. But, Info has a link and the user might want to click it. I can delay the Info's slide up but I can't stop it after I reach Info.

This is the code I'm using.

$("#button").hover(function(){
    $("#info").slideDown("fast");
}, function(){ //the handlerOut
    $("#info").delay(1000).slideUp("fast");
});

How can I tell Button not to slideUp Info after I hover it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰葑 2024-11-26 03:49:37

您可以使用 stop() 停止任何当前排队的动画。请注意,stop() 会停止任何当前正在运行的动画,无论其当前位于何处,因此在这种情况下,我们需要停止动画显示该元素。

(顺便说一句,在解决行为问题之前,您可能已经想使用 stop() 来防止用户将鼠标快速移入和移出按钮时出现弹跳效果):

$("#button").hover(function(){
    $("#info").stop(true,true).slideDown("fast");
}, function(){ //the handlerOut
    $("#info").stop(true,true).delay(1000).slideUp("fast");
});

然后,获取您想要的行为,我们需要向 #info 添加一个悬停处理程序来停止当前动画,然后根据需要显示或隐藏 info 元素。由于我们已经有一个处理程序可以执行此操作,因此您只需将 #info 选择器添加到悬停调用即可:

$("#button, #info").hover(function(){
    $("#info").stop(true,true).slideDown("fast");
}, function(){ //the handlerOut
    $("#info").stop(true,true).delay(1000).slideUp("fast");
});

这是一个 工作 jsfiddle

You can use stop() to stop any currently queued animation. Note that stop() stops any currently running animation wherever it currently is, so in this case we'll need to stop the animation and show the element.

(As an aside, before solving the behaviour issue, you might already want to use stop() to prevent bouncing effects if the user mouses in and out of the button very quickly):

$("#button").hover(function(){
    $("#info").stop(true,true).slideDown("fast");
}, function(){ //the handlerOut
    $("#info").stop(true,true).delay(1000).slideUp("fast");
});

Then, to get the behaviour you want, we need to add a hover handler to #info that stops the current animation, and then either shows or hides the info element, as appropriate. Since we've already got a handler that does that, you can just add the #info selector to the hover call:

$("#button, #info").hover(function(){
    $("#info").stop(true,true).slideDown("fast");
}, function(){ //the handlerOut
    $("#info").stop(true,true).delay(1000).slideUp("fast");
});

Here's a working jsfiddle

心安伴我暖 2024-11-26 03:49:37

解决这个问题的通常方法是在相关元素隐藏之前设置一个延迟,如果用户在延迟期间悬停该元素,则完全取消隐藏。

$("#button").hover(function(){
    $("#info").slideDown("fast");
}, function(){ //the handlerOut
    $('#info').data('timeout', setTimeout(function(){
          $("#info").slideUp("fast");
    },1000) );
});

$('#info').hover(function(){
    clearTimeout( $(this).data('timeout') ); // cancel the delayed code execution
}, function(){ //the handlerOut
    $(this).data('timeout', setTimeout(function(){
          $("#info").slideUp("fast");
    },1000) );
});

演示位于 http://jsfiddle.net/gaby/VjLM2/

The usual solution to this problem, is to set a delay before the related element hides as well, and if the user hovers that element during the delay, to cancel the hiding completely.

$("#button").hover(function(){
    $("#info").slideDown("fast");
}, function(){ //the handlerOut
    $('#info').data('timeout', setTimeout(function(){
          $("#info").slideUp("fast");
    },1000) );
});

$('#info').hover(function(){
    clearTimeout( $(this).data('timeout') ); // cancel the delayed code execution
}, function(){ //the handlerOut
    $(this).data('timeout', setTimeout(function(){
          $("#info").slideUp("fast");
    },1000) );
});

Demo at http://jsfiddle.net/gaby/VjLM2/

↙厌世 2024-11-26 03:49:37

http://jsfiddle.net/

<a href="javasctipt:" class="hoverNav" target="info">
    show info
</a>

<div id="info" class="hoverNavDescription">
Iam info text
</div>






 var curVisible="";
jQuery(function(){
    jQuery('.hoverNav').bind('mouseover',function(){
         var elm=jQuery(this),
             targetName=elm.attr('target')
             ;
        curVisible=targetName;
        jQuery('#'+targetName).slideDown();
        jQuery(window).bind('click',handleMouseOut)

    });

function handleMouseOut(e)
{
    if($(e.target).attr('id')!=curVisible)
    {
        jQuery('#'+curVisible).slideUp();
        curVisible="";
        jQuery(window).unbind(handleMouseOut);
    }
}

});

.hoverNavDescription
{
    display:none;
    background-color:red;
    height:100px;
    width:100px;
}

http://jsfiddle.net/

<a href="javasctipt:" class="hoverNav" target="info">
    show info
</a>

<div id="info" class="hoverNavDescription">
Iam info text
</div>






 var curVisible="";
jQuery(function(){
    jQuery('.hoverNav').bind('mouseover',function(){
         var elm=jQuery(this),
             targetName=elm.attr('target')
             ;
        curVisible=targetName;
        jQuery('#'+targetName).slideDown();
        jQuery(window).bind('click',handleMouseOut)

    });

function handleMouseOut(e)
{
    if($(e.target).attr('id')!=curVisible)
    {
        jQuery('#'+curVisible).slideUp();
        curVisible="";
        jQuery(window).unbind(handleMouseOut);
    }
}

});

.hoverNavDescription
{
    display:none;
    background-color:red;
    height:100px;
    width:100px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文