2

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

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

1 Comment

I think this still has the same problem. The event that should be passed in on your first line (change function() to function(e)) doesn't have e.pageY or e.pageX defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.