获取元素距离页面的距离

function offset(element){

    var pos={left:0, top:0};

    var parents=element.offsetParent;

    pos.left+=element.offsetLeft;
    pos.top+=element.offsetTop;

    while(parents && !/html|body/i.test(parents.tagName)){

        pos.left+=parents.offsetLeft;
        pos.top+=parents.offsetTop;

        parents=parents.offsetParent;
    }
    return pos;
}

//var obj=offset(son);
//console.log(obj);

你可能感兴趣的:(javascript)