Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get mouse position in jQuery without mouse-events?

I would like to get current mouse position but I don't want to use:

$(document).bind('mousemove',function(e){          $("#log").text("e.pageX: " + e.pageX + ", e.pageY: " + e.pageY);  });  

because I just need to get the position and process the information

like image 685
Martin Vseticka Avatar asked Dec 23 '10 09:12

Martin Vseticka


People also ask

How do I find my current mouse position?

To get the current mouse position we are going to trigger a mouse event. In this case we will use 'mousemove' to log the current X and Y coordinates of the mouse to the console.

How do I get my mouse position in Qt?

To set or get the position of the mouse cursor use the static methods QCursor::pos() and QCursor::setPos().


1 Answers

I don't believe there's a way to query the mouse position, but you can use a mousemove handler that just stores the information away, so you can query the stored information.

jQuery(function($) {     var currentMousePos = { x: -1, y: -1 };     $(document).mousemove(function(event) {         currentMousePos.x = event.pageX;         currentMousePos.y = event.pageY;     });      // ELSEWHERE, your code that needs to know the mouse position without an event     if (currentMousePos.x < 10) {         // ....     } }); 

But almost all code, other than setTimeout code and such, runs in response to an event, and most events provide the mouse position. So your code that needs to know where the mouse is probably already has access to that information...

like image 123
T.J. Crowder Avatar answered Oct 16 '22 10:10

T.J. Crowder