sasa
sasa

Reputation: 2443

How to verify that the mouse over an element?

Do you know how to check whether the mouse is over an element?

Somethnig like this?

setTimeout(function() {
    if($(this).mouseover()) {  // this not work
        return false;   
    } else {
        $(this).hide();
    }
}, 1000);

Thanks.

Upvotes: 0

Views: 329

Answers (3)

James
James

Reputation: 111870

I'm assuming you're operating within a closure where 'this' represents a single element:

var mouseovered = false,
    myElem = this;

$(myElem)
    .mouseover(function(){
        mouseovered = true;
    })
    .mouseout(function(){
        mouseovered = false;
    });

setTimeout(function() {
   if(mouseovered) {  
        return false;   
    } else {
        $(myElem).hide();
    }
}, 1000);

Notice that I'm using "myElem" instead of the "this" keyword, which, in the context of the setTimeout callback will be a reference to the Window object - obviously not what you want.

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104168

You could use something like this:

var isMouseOver = false;

$(myitem).hover(function() {isMouseOver = true;}, 
                function() {isMouseOver = false;});

Upvotes: 2

Ian Devlin
Ian Devlin

Reputation: 18870

Use the onmouseover event on the element in question to call a function to hide the element in question (as this seems to be what you want to do).

Upvotes: 0

Related Questions