Created
December 9, 2012 23:22
-
-
Save mrdoob/4247472 to your computer and use it in GitHub Desktop.
Is it possible to have event.offsetX always relative to the element with the listener?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * +--------------------+ | |
| * | element1 | | |
| * | +----------+ | | |
| * | | element2 | | | |
| * | +----------+ | | |
| * +--------------------+ | |
| * | |
| * (element2 is a child of element1) | |
| * | |
| * Is it possible to have event.offsetX | |
| * always relative to element1 regardless of | |
| * whether element1 or element2 is clicked? | |
| * | |
| */ | |
| element1.addEventListener( 'click', function ( event ) { | |
| console.log( event.offsetX ); | |
| }, false ); | |
| // I would like to stop using this workaround... | |
| element1.addEventListener( 'click', function ( event ) { | |
| var offsetX = event.offsetX; | |
| var node = event.target; | |
| while ( node !== this ) { | |
| offsetX += node.offsetLeft; | |
| node = node.parentNode; | |
| } | |
| console.log( offsetX ); | |
| }, false ); |
Author
Author
Meh... event.x is undefined in Firefox :(
what about something like:
console.log( (event.clientX + document.documentElement.scrollLeft) || (event.x + this.scrollLeft) );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution:
Thanks @lemieuxster!