获取定位元素相对于浏览器窗口的绝对位置

//获取定位元素相对于浏览器窗口的绝对位置

function _getAbsolutePosition(obj) {

            //如果函数没有传入值的话返回对象为空的
            if(!obj)return null;
            var w = obj.offsetWidth, h = obj.offsetHeight;
            //从目标元素开始向外遍历,累加top和left值
            var t, l;
            for (t = obj.offsetTop, l = obj.offsetLeft; obj = obj.offsetParent;) {
                t += obj.offsetTop;
                l += obj.offsetLeft;
            }
            var r = document.body.offsetWidth - w - l;
            var b = document.body.offsetHeight - h - t;

            返回定位元素的坐标集合
            return {width: w, height: h, top: t, left: l, right: r, bottom: b};
        }

转载于:https://my.oschina.net/u/3283353/blog/1475125

你可能感兴趣的:(获取定位元素相对于浏览器窗口的绝对位置)