关闭

H5获取元素相对于页面位置

瞎溜达 1年前 ⋅ 75 阅读
function getPosition(element) {
    if(element.parentNode === null) {
		return false;
	}
    var parent = null;
    var pos = [];
    var box;
    if(element.getBoundingClientRect){
        box = element.getBoundingClientRect();
        return {left:box.left , top:box.top};
    }else{
        pos = [element.offsetLeft, element.offsetTop];
        parent = element.offsetParent;
        if (parent != element) {
            while (parent) {
                pos[0] += parent.offsetLeft;
                pos[1] += parent.offsetTop;
                parent = parent.offsetParent;
            }
        }
    }
    return {left:pos[0], top:pos[1]};
}

全部评论: 0

    我有话说: