复制代码
1
2
3
4
5
6
7
8
9
10
11
12function getElementLeft(element) { var actualLeft = element.offsetLeft; var current = element.offsetParent; while (current!==null) { actualLeft += current.offsetLeft; current = current.offsetParent; } return actualLeft; }
取得元素的左偏移量;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12function getElementTop(element) { var actualTop = element.offsetTop; var current = element.offsetParent; while (current!==null) { <span style="white-space:pre"> </span>actualTop += current.offsetTop; current = current.offsetParent; } return actualTop; }
取得元素的上偏移量;
利用offsetParent属性在Dom层次中逐级向上回溯,将每个层次偏移量合计。
最后
以上就是迅速发卡最近收集整理的关于取得元素的左和上偏移量的方法的全部内容,更多相关取得元素内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复