Rich
Rich

Reputation: 12663

Trigger JavaScript mousemove without moving mouse but get mouse coordinates

I'd like to register a mousemove callback and immediately trigger that callback with the current mouse x/y position. Currently I'm doing this:

$('body').mousemove(hoverFunction).mousemove();

But hoverFunction's event receives undefined values for pageX and pageY as documented. Is there a clean way I can handle this besides having a mousemove callback that updates a global mouse position variable?

Upvotes: 2

Views: 4736

Answers (2)

tenedor
tenedor

Reputation: 781

You need to create a jQuery.Event object and set pageX and pageY properties on it directly. Then trigger this event on $(document). See this StackOverflow question.

Upvotes: 3

SeanCannon
SeanCannon

Reputation: 77976

$('body').bind('mousemove',function(e){   
    hoverFunction(e);
});

$(function(){
    $('body').trigger('mousemove');
});

Upvotes: 0

Related Questions