The other answers that use getBoundingClientRect
work in most cases, but it calculates from the viewport, not the DOM's actual 0,0. So if the parent element has negative coords, and is off screen, getBoundingClientRect
will return the wrong coordinates.
It also ignores margin
, padding
, and borderWidth
, so if you need to be very precise:
trueLeft = rect.left - margin.left - padding.left - borderWidth - offsetX
to get the other values could use a RegExp
:
'translate3d(256px, 512px, 0px)'.replace(/translate3d|px|\(|\)/gi, '').split(',');
// { x: trueLeft, y: translate3d[1], z: translate3d[2] }