对于不同的元素,不同的浏览器,offsetParent含义不同,有时,指的是直接包含的元素,有时指的是HTML元素,有时不存在offsetParent.

如果所研究的元素没有offsetParent,仅计算该元素自身的偏移位置,否则要将元素的偏移值加到元素的offsetParent偏移值,再重复上面步骤,一级一级递归。

function getPosition(theElement){

    var positionX = 0;
    var positionY = 0;  
    
    while(theElement != null){
         positionX += theElement.offsetLeft;
         positionY += theElement.offsetTop;
         theElement = theElement.offsetParent;
  
    }

     return [positionX,positionY];
}

Mac下的IE5还考虑了body页变空白或边框距,精确地度量尺度,应该使body上的页边空白和边框距为0.

 

相关文章:

  • 2021-12-26
  • 2021-09-26
  • 2021-04-12
  • 2021-07-30
  • 2022-12-23
  • 2021-11-09
  • 2021-07-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2022-02-23
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案