JS获取页面对象的绝对位置

// get absolute Left position
function getAbsoluteLeft(ob){
if(!ob){return null;}
    var mendingOb = ob;
    var mendingLeft = mendingOb .offsetLeft;
    while( mendingOb != null && mendingOb .offsetParent != null && mendingOb .offsetParent.tagName != "BODY" ){
      mendingLeft += mendingOb .offsetParent.offsetLeft;
      mendingOb = mendingOb .offsetParent;
    }
return mendingLeft ;
}

// get absolute TOP position
function getAbsoluteTop(ob){
if(!ob){return null;}
var mendingOb = ob;
var mendingTop = mendingOb .offsetTop;
while( mendingOb != null && mendingOb .offsetParent != null && mendingOb .offsetParent.tagName != "BODY" ){
    mendingTop += mendingOb .offsetParent.offsetTop;
    mendingOb = mendingOb .offsetParent;
}
return mendingTop ;
}

 

你可能感兴趣的:(js)