getBoundingClientRect

现在主流浏览器均已支持getBoundingClientRect。
获取dom元素相对client(窗口客户区域)的位置。类似鼠标事件中的clientX,clientY。只是getBoundingClientRect的属性更多top,right,bottom,left,width,height。
如果有滚动条,则随着滚动条的拖动,则上表示位置的属性会改变。
如果要取得某个dom相对文档树的根结点的相对位置,
使用getBoundingClientRect的left,top加上scrollLeft,scrollTop。
而不用遍历offsetParent,把offsetLeft,borderLeftWidth或offsetTop,borderTopWidth相加在一起。

你可能感兴趣的:(浏览器)