If Mouseover Div Equals False - JQueryJavascript - Stack Overflow

I'm trying to get this to work:if($('#myDiv').hover() == false) {myFunction();}Not gett

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. 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: /

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

p.s. Most inventive answer marked as right and upvotes all round.

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. 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/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

p.s. Most inventive answer marked as right and upvotes all round.

Share Improve this question edited Mar 23, 2011 at 21:46 logic-unit asked Mar 23, 2011 at 20:51 logic-unitlogic-unit 4,31312 gold badges49 silver badges74 bronze badges 3
  • Execute a function when the mouse isn't over a div. – logic-unit Commented Mar 23, 2011 at 21:07
  • You mean when the mouse leaves from over the div? Or execute a function over and over while the mouse is not over the div? – Jage Commented Mar 23, 2011 at 21:09
  • The latter, execute the function whenever the mouse is not over the div. Don't execute when it is. – logic-unit Commented Mar 23, 2011 at 21:13
Add a ment  | 

4 Answers 4

Reset to default 2

Without knowing your ultimate intent, you could wire up a hover on the document and check the current 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.

This code sample sets up a global js variable to store the hover state of the div. Then I use jquery hover to toggle that between true / false. Then, we just fire off a function every 10ms that checks the hover state. 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

The hover function takes 2 callback functions:

('#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.

If you're only interested in doing something when the hover stops, you can use the mouseout() function:

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

Your first call could never work:

$('#myDiv').hover()

This actually says "trigger the hover event on the element". 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745390364a4625634.html

相关推荐

  • If Mouseover Div Equals False - JQueryJavascript - Stack Overflow

    I'm trying to get this to work:if($('#myDiv').hover() == false) {myFunction();}Not gett

    13小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信