getPosition (node) {
let left = node.offsetLeft // 获取元素相对于其父元素的left值var left
let top = node.offsetTop
let current = node.offsetParent // 取得元素的offsetParent
// 一直循环直到根元素
while (current != null) {
left += current.offsetLeft
top += current.offsetTop
current = current.offsetParent
}
return {
'left': left,
'top': top
}
}
当前对象所在的top和left
最新推荐文章于 2022-07-08 16:32:37 发布
博客展示了一段JavaScript代码,定义了一个名为getPosition的函数,用于获取元素相对于文档的位置。函数通过不断累加元素及其offsetParent的offsetLeft和offsetTop值,最终返回元素的left和top坐标。
75万+

被折叠的 条评论
为什么被折叠?



