简体   繁体   English

如果Mouseover Div等于False-jQuery / Javascript

[英]If Mouseover Div Equals False - JQuery / Javascript

I'm trying to get this to work: 我正在尝试使它起作用:

if($('#myDiv').hover() == false) {
  myFunction();
}

Not getting much in the way of errors in Chrome or Firebug consoles. Chrome或Firebug控制台的错误处理方式不多。 I've had a look at some other posts, and there was an answer that used something like: 我看了其他一些帖子,有一个答案是这样的:

if($('#myDiv').is(':hover') == false) {
  myFunction();
}

However this also doesn't work. 但是,这也不起作用。

Here's a jsfiddle if that helps: http://jsfiddle.net/yuwPR/2/ 如果有帮助,请使用jsfiddle: http : //jsfiddle.net/yuwPR/2/

Any ideas greatly appreciated. 任何想法表示赞赏。

Thanks 谢谢

EDIT: 编辑:

Thanks for the answers, I wasn't able to get anything working. 感谢您的回答,我什么都做不了。 I'm thinking it might not be possible. 我认为这可能是不可能的。 Oh well, I'll try something else! 哦,我会尝试其他的!

Thanks again 再次感谢

ps Most inventive answer marked as right and upvotes all round. ps最有创造力的答案标记为正确,并全面投票。

Without knowing your ultimate intent, you could wire up a hover on the document and check the current target.id 在不知道最终意图的情况下,您可以将鼠标悬停在document并检查当前的target.id

$(document).mouseover(function(event) {
    if (event.target.id == "myDiv") {
        $("body").css("background-color", "red"); //over the div so change the color
        return;
    }
    $("body").css("background-color", "green"); //no on the div
});

code example on jsfiddle . jsfiddle上的代码示例。

The hover function takes 2 callback functions: 悬停函数需要2个回调函数:

('#myDiv').hover(function () {
        // function to call when hovering 
    }, 
    function () { 
        myFunction(); 
    }
);

So, when hovering is "false", ie, on mouse out, the second function will be called. 因此,当悬停为“ false”时,即在鼠标移出时,将调用第二个函数。

If you're only interested in doing something when the hover stops, you can use the mouseout() function: 如果只对悬停时感兴趣的事情感兴趣,可以使用mouseout()函数:

$('#myDiv').mouseout(function() {
        myFunction();
    }
);

Your first call could never work: 您的第一个电话永远无法使用:

$('#myDiv').hover()

This actually says "trigger the hover event on the element". 这实际上是“触发元素上的hover事件”。 It does not check to see if your user is currently hovering over the element. 它不会检查您的用户当前是否将鼠标悬停在元素上。

Your second formulation should work: 您的第二个公式应该起作用:

$('#myDiv').is(':hover')

This checks to see if the element currently has the mouse hovering over it. 这将检查元素当前是否有鼠标悬停在该元素上。 However, it doesn't seem to work on document load. 但是,它似乎不适用于文档加载。 An example that works can be seen here . 在这里可以看到一个有效的示例。 If you can clarify what you're trying to do, it might be possible to find some working code in this style. 如果您可以弄清楚自己要做什么,则可能可以找到这种风格的工作代码。

This code sample sets up a global js variable to store the hover state of the div. 此代码示例设置了一个全局js变量来存储div的悬停状态。 Then I use jquery hover to toggle that between true / false. 然后,我使用jquery悬停在true / false之间切换。 Then, we just fire off a function every 10ms that checks the hover state. 然后,我们仅每10毫秒触发一次检查悬停状态的函数。 Currently I am just setting the window status telling you if you're hovered or not. 目前,我只是设置窗口状态,以告诉您是否悬停。

var _MOUSEOVER_IN_PROGRESS = false; //stores the hover state
function isover(){
    if(_MOUSEOVER_IN_PROGRESS){
        window.status = 'Still over!';
    } else {
        window.status = 'You are not hovering on me!';
    }
    setTimeout("isover()",10); //checking every 10ms! 
}



$(document).ready(function(){
    isover();
    $('#mydiv').hover(
        function(){ _MOUSEOVER_IN_PROGRESS = true; }, 
        function(){ _MOUSEOVER_IN_PROGRESS = false; }
    );
});

Edited my code! 编辑了我的代码! My mydiv hover catch was not wrapped in a document ready 我的mydiv悬停捕获未包装在准备好的文档中

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

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