Js获取元素在页面里的坐标位置和宽高

 1 //获取元素在页面里的位置和宽高
 2 function getPosition(obj) {
 3     var top = 0,
 4         left = 0,
 5         width = obj.offsetWidth,
 6         height = obj.offsetHeight;
 7 
 8     while(obj.offsetParent){
 9         top += obj.offsetTop;
10         left += obj.offsetLeft;
11         obj = obj.offsetParent;
12     }
13 
14     return {"top":top,"left":left,"width":width,"height":height};
15 }

 

原文地址:https://www.cnblogs.com/dtdxrk/p/2577284.html