Determine mouse position outside of events (using jQuery)?

pbz picture pbz · Jul 15, 2009 · Viewed 47.3k times · Source

I need to get a hold of the absolute mouse position / coordinates (X and Y) using (preferably) jQuery like in this tutorial but outside of any JavaScript event. Thank you.

Answer

Chetan S picture Chetan S · Jul 15, 2009

Not possible. You can however use the same approach in the tutorial to store the position in a global variable and read it outside the event.

Like this:

jQuery(document).ready(function(){
   $().mousemove(function(e){
      window.mouseXPos = e.pageX;
      window.mouseYPos = e.pageY;
   }); 
})

You can now use window.mouseXPos and window.mouseYPos from anywhere.