esedic
6/16/2017 - 1:07 PM

Get element coordinates/position with Javascript

Get element coordinates/position with Javascript

function getOffset(el) {
    var _x = 0;
    var _y = 0;
    while( el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
    }
    return {top: _y, left: _x};
}
var x = getOffset(document.getElementById('myElem').left;
var y = getOffset(document.getElementById('myElem').top;