0 points

javascript - Retrieve the position (X,Y) of an HTML element relative to the browser window - Stack Overflow


dashboard

Sun Jun 21 2020 16:08:00 GMT+0000 (UTC)

Posted by @mishka #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(%27yourElId%27) ).left; 
content_copy Copy