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
I used this method:
In this way I'll always have the distance from the top saved in y and the distance from the left saved in x.
i came across this, tot it would be nice to share... what do you guys think.
and boom, there we have it..
Moreover,
mousemove
events are not triggered if you perform drag'n'drop over a browser window. To track mouse coordinates during drag'n'drop you should attach handler fordocument.ondragover
event and use it's originalEvent property.Example:
You can't read mouse position in jQuery without using an event. Note firstly that the
event.pageX
andevent.pageY
properties exists on any event, so you could do:Your other option is to use a closure to give your whole code access to a variable that is updated by a mousemove handler:
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.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...