1

So I want to be able to trigger my mousemove function in jquery using $().mousemove(), but I want to also be able to pass the current mouse state as a parameter, so my mousemove function knows the mouse x and y coordinates.

I understand that a possible workaround is just to save the x and y coordinates and create a function that directly uses these coordinates, but I wanted to know if there was a way to just get the current mouse event.

Example:

$(document).mousemove(function(e) {
    var x = e.pageX;
    var y = e.pageY;
    // do stuff with x and y
});

function trigger_mousemove() {
    $(document).mousemove(/** here is where I want something to be able to put in */);
}

Thanks in advance!

Ofek Gila
  • 693
  • 1
  • 10
  • 21

1 Answers1

1

Use a closure to give your whole code access to a variable that is updated by a mousemove handler:

var mouseX, mouseY;
$(document).mousemove(function(e) {
    mouseX = e.pageX;
    mouseY = e.pageY;
}).mouseover(); // call the handler immediately

// do something with mouseX and mouseY

cytation from user lonesomeday if you like the answer give him credit in link below. See How to get mouse position in jQuery without mouse-events? for reference

Community
  • 1
  • 1
DevWL
  • 17,345
  • 6
  • 90
  • 86
  • Thanks. I was hoping there would be way to actually get the object, but I guess there isn't. Thanks a bunch – Ofek Gila Sep 18 '15 at 00:54