when i call a function, i get: window.event.clientX and window.event.clientY from a global function, paste.
Im trying to paste at the coordinates, but they are off by some info i am working to resolve.
i was just going to take:
window_data: {top,left}
and substract:
stage_div_offset: {top,left}
to get the actual offset but that seems to also be off by a bit. Around 40 pixels in both X and Y directions. I assumed this is due to maybe margin or padding or something like that.
What i was really curious of is, Is there a way to get the X
and Y
of the mouse in reference to a div or other htmlElement
?
I wasnt sure if there was a function which i could pass in a selector, or if using something like jquery, there would actually be a function or something for the selector called: mouse.
I do feel this following Topic is relevant, though not sure if it makes mine a dup:
jQuery get mouse position within an element
Edit: Originally i was doing the following code:
var stage = $("#stage").offset(),
results = {
left: window.event.clientX - stage.left,
top: window.event.clientY - stage.top
};
Edit 2: none of the current answers seem to be working, partially because my browser is not recognizing the mouseEvent, so it cant get the screens location.
I wrote the following to try to get the mouselocation
var MouseLocation = {};
MouseLocation.Left = 0;
MouseLocation.Top = 0;
MouseLocation._event;
MouseLocation.get_position = function () { return { left: MouseLocation.Left, top: MouseLocation.Top }; }
MouseLocation.attach = function () {
MouseLocation._event = function (e) {
var loc = { left: e.clientX, top: e.clientY }
MouseLocation.Left = loc.left;
MouseLocation.Top = loc.top;
};
$("#stage").on("mousemove", MouseLocation._event);
}
MouseLocation.detach = function () {
$("#stage").off("mousemove", MouseLocation._event);
}
MouseLocation.ping = function () {
MouseLocation.attach();
$("#stage").mousemove();
MouseLocation.detach();
}
so that way, inside of my event, i can just say:
MouseLocation.ping(); MouseLocation.get_position();
but it doesnt seem to like: $("#stage").on("mousemove")