function getRec(ele) {
var _t = document.documentElement.clientTop,
_l = document.documentElement.clientLeft,
rect = ele.getBoundingClientRect();
return {
top: rect.top - _t,
right: rect.right - _l,
bottom: rect.bottom - _t,
left: rect.left - _l,
width: rect.width||rect.right - rect.left,
height: rect.height|| rect.bottom - rect.top
}
}
使用方式:
getRec(document.getElementById("xxx"));
输出:
{"top":xx,"right":xx,"bottom":xx,"left":xx,"width":xx,"height":xx}
获取元素位置函数
最新推荐文章于 2024-06-07 10:30:53 发布