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!

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I was hoping there would be way to actually get the object, but I guess there isn't. Thanks a bunch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.