1

I'm trying to get the XY position of the mouse in a parent element. That part is simple, what's throwing me off is when there's a nested element, it then grabs the XY position of the mouse in that nested element.

I created a JS Fiddle with a very simple example: http://jsfiddle.net/htenchwv/

You can see how I'm pulling the position with this

$(".parent").mousemove(function(event){
    $(".debug").html("X: "+ event.offsetX +"<br>Y: "+ event.offsetY);
});

You can see in either the X or Y position, the numbers jump down when you move into the child element. I'm at a loss for why or how I can prevent that behavior and gather ONLY the XY mouse in the parent element

John Sly
  • 763
  • 1
  • 10
  • 31

1 Answers1

2

You can use client X and Y, combined with .offset()

Updated demo

$(document).ready(function($){
    $(".parent").mousemove(function(event){
        var pOffset = $(".parent").offset(),
            px = event.clientX - pOffset.left,
            py = event.clientY - pOffset.top;
        $(".debug").html("X: "+ px +"<br>Y: "+ py);
    });
});

HTH

-Ted

Community
  • 1
  • 1
Ted
  • 14,757
  • 2
  • 41
  • 58