¿Puede obtener ClientX && ClientY en referencia a un selector en lugar de la ventana?

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 e 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 obtiene la posición del mouse dentro de un elemento

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 

Editar 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.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 () {

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")

preguntado el 28 de mayo de 14 a las 12:05

2 Respuestas

Puedes usar posición () para:

Obtenga las coordenadas actuales del primer elemento en el conjunto de elementos coincidentes, en relación con el padre desplazado.

contestado el 28 de mayo de 14 a las 12:05

I added some code. Right now, i was just subracting the of stage.offset() from event.client*. Are you saying that instead i should do event.client* - {stage.parent().offset() + stage.position()} - Caído

It seems that one of the issues im having at the moment is that when i paste, it doesnt recognize what: window.event.clientX etc is. Am i missing something? Right now, I click and copy, but im just trying to get the coords of whereever the position of the mouse is. It seems like when i do paste, it cant find my mouse or something. I think it is because event is being used for click stuff, so a lot of the things related to the mouse is 0 or undefined. - Caído

@RodringoDela above was making not that offset accounts for padding, margin, borders, etc. Im thinking the deviation might be related to that, but i cant find much more - Caído

You can just take the document coordinates of the mouse and substract the coordinates of the element:

relX = e.clientX-element.offset().left;
relY = e.clientY-element.offset().top;


The advantage to offset() is that margin and paddings are included into the calculations.

contestado el 28 de mayo de 14 a las 12:05

since i have margin and paddings etc, would it not make more sense to use position then? - Caído

Oh I'm sorry. I was mistaken, it's the advantage of offset(). - RodrigoDela

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.