Jquery:如果 mouseleave = true 然后做这个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4830829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:00:29  来源:igfitidea点击:

Jquery: if mouseleave = true then DO THIS

jqueryhovermouseovermouseleave

提问by android.nick

Simple question, how would I accomplish this functionality in Jquery: Test whether the mouse is hovering over .myBox

简单的问题,我将如何在 Jquery 中完成此功能:测试鼠标是否悬停 .myBox

    if ($(".myBox").mouseleave = true) {
        DO SOMETHING
    } else {something else}

OR

或者

    if ($(".myBox").mouseover = false) {
        DO SOMETHING
    } else {Something else}

NOTE: im looking for an IF statement

注意:我正在寻找 IF 语句

回答by Marcus Whybrow

jQuery provides the ismethod for checking conditions with regard to a jQuery object. In your case you can check for the :hoverCSS pseudo class:

jQuery 提供了is检查与 jQuery 对象有关的条件的方法。在您的情况下,您可以检查:hoverCSS 伪类:

$('.myBox').is(':hover') === true

Havre a look at this demo, try clicking the button (which will alert true) and the tabbing to and hitting enter on the button (without the mouse, it will return false).

看看这个演示,尝试点击按钮(它会true发出警报),然后在按钮上点击并点击回车(没有鼠标,它会返回false)。

DEMO:http://jsfiddle.net/marcuswhybrow/LL5JD/

演示:http : //jsfiddle.net/marcuswhybrow/LL5JD/

回答by Scoobler

Take a look at jQuery Mouse Over

看看jQuery 鼠标悬停

$(".my_box").mouseover(function(){
    // Mouse over...
});

$(".my_box").mouseout(function(){
    // Mouse left...
});

Here is an example, adding a border to an image when hovering over it, and removing it after x amount of time if its not been re-hovered: See it working here

这是一个示例,将鼠标悬停在图像上时为其添加边框,如果未重新悬停,则在 x 时间后将其删除:参见此处工作

var hover_off = false;
var hover_count = 1000;

$(".my_box").mouseover(function() {
    hover_off = false;
    $(this).addClass('hovering');
});

$(".my_box").mouseout(function() {
    hover_off = true;
    setTimeout(myMouseOut, hover_count);
});

function myMouseOut() {
    if (hover_off) {
        $(".my_box").removeClass('hovering');
    }
}

回答by Oliver A. Keith

That's right, $('.myBox').is(':hover') used with jQuery 1.5.1 throws the error, but in my tests only in Opera Browser (11.01): Uncaught exception: Syntax error, unrecognized expression: hover

没错,与 jQuery 1.5.1 一起使用的 $('.myBox').is(':hover') 抛出错误,但在我的测试中仅在 Opera 浏览器 (11.01) 中:未捕获的异常:语法错误,无法识别的表达式:悬停

A workaround is using a negated expression: $('.myBox').is('not(:hover)') That worked fine while my tests in Opera 11, FF4, IE7, Chrome 5.

一种解决方法是使用否定表达式:$('.myBox').is('not(:hover)') 在我在 Opera 11、FF4、IE7、Chrome 5 中进行测试时效果很好。

回答by Robert Schmidt

This did the trick for me:

这对我有用:

var hover = false;
$('.someClass').each(function(i,e){
    hover = !hover ? $(e).is(':hover') : true;
});

if (hover)
    // do something
else
    // do something else

回答by TheVillageIdiot

$(".myBox").hover(
           function(){
               //DO SOMETHING ON MOUSE ENTER
           },
           function(){
               //DO SOMETHING ON MOUSE LEAVE
           }
 });

回答by Crayon Violent

回答by Syl

use this: http://plugins.jquery.com/project/enterleave-events

使用这个:http: //plugins.jquery.com/project/enterleave-events

$('.myBox').bind('enter leave',function() {
  // do something, use $(this) to get the object
});

回答by Gabriele Petrioli

Act on the mouseenterand mouseleaveevents.

mouseentermouseleave事件采取行动。

Or use the hoverwhich can handle both of them..

或者使用hover可以处理它们的 ..

$('.myBox').hover(
   function(e){
     //do the mouseenter things here...
   },
   function(e){
     //do the mouseleave things here...
   }
);

Example at http://www.jsfiddle.net/gaby/ECYD4/

示例在http://www.jsfiddle.net/gaby/ECYD4/