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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rich
  • 12,068
  • 9
  • 62
  • 94

2 Answers2

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.

Community
  • 1
  • 1
tenedor
  • 671
  • 7
  • 15
0
$('body').bind('mousemove',function(e){   
    hoverFunction(e);
});

$(function(){
    $('body').trigger('mousemove');
});
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • 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. – Rich Jul 15 '11 at 22:16