Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mousemove vs mouseover vs something else

I need to bind a mouse event to an area of an image.

Just think of the Facebook picture tag for a second, when you hover the face, it shows you the name.

I made a very similar thing but with maps and city names, like this:

$('img#mapaMundi').bind('mousemove', function(e) {
    x = getX();
    y = getY();
    var found = find(x, y);

    if (found == undefined) {
        console.log('There is no tagged city for this position');
    } else {
        show(found);
    }
});

And this works great, it shows the desired tag (with animation and stuff) but only while mouse is moved in the area, so if you move to the area and leave the mouse there (as it's not moving) it will dissapear.

If I use .bind('mouseover') it won't work because when you hover the image it's always in one of the edges.

What would you suggest?

like image 593
Toni Michel Caubet Avatar asked Nov 25 '11 12:11

Toni Michel Caubet


2 Answers

on mouseover setInterval with a function that will check mouse position each second, and on mouseout clear that interval

If you use jQuery, .hover() provides both mouseover and mouseout

like image 191
Maxim Krizhanovsky Avatar answered Sep 22 '22 23:09

Maxim Krizhanovsky


You can combine maybe mouseover or mouseenter and mousemove

http://api.jquery.com/mousemove/

http://api.jquery.com/mouseenter/

So when mouseenter -> mousemove

mouseleave -> do nothing?

var int = null;
$('#element').hover(function() {
int = setInterval(someFunc, 100);
}, function() {
clearInterval(int);
});

function someFunc() {
DO YOUR MOUSEMOVE THINGS
}
like image 37
Daniel Ruf Avatar answered Sep 24 '22 23:09

Daniel Ruf