I'm trying to get x and y coordinates of mouse click event within the relative container. There are already several question on this topic on stackoverflow, for example jQuery get mouse position within an element So I've tried to translate the event.pageX and event.pageY coordinates from the event into a mouse position relative to the parent as it was in the example:
$("#something").click(function(e){
var parentOffset = $(this).parent().offset();
//or $(this).offset(); if you really just want the current element's offset
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
});
In most cases it works just fine. But it does not work on Android (Nexus 5) in Chrome WHEN we scroll content. It also does not work in Desktop Chrome in emulation mode. The value of pageY is strange and I do not understand why. Could someone help me with that? Maybe we can't use this approach? Or there is a quick fix for that?
Here is the Plunk: http://plnkr.co/edit/Ul2bd8v1iLRvEyGxjv4b?p=info Just scroll to the red rectangle and click on it. It would output the coordinates of mouse relative to the top left corner of that rectangle. If you turn on emulation in Chrome (Nexus 5 for example) or open that Plunk on Android device in Chrome (tried v. 4.4.2 and 4.2.2, ONLY Chrome) you will get an incorrect position.
Thanks in advance!