JS获取对象在窗口中的位置

//JS得到对象在页面中离左边的绝对位置
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 ;
}

//JS得到对象在页面中离顶部的绝对位置
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)