$(this).position和getBoundingClientRect()之间的区别

时间:2018-12-04 10:37:49

标签: javascript jquery

jQuery提供了position函数,该函数具有顶部和左侧的值。 两种原始javascript都有getBoundingClientRect()

我没什么区别。
当我检查时,这是价值差异。

getBoundingClientRect() enter image description here

如何将jQuery的position转换为getBoundingClientRect
如您所见,x是236,但左边是200。

1 个答案:

答案 0 :(得分:2)

jQuery .position()获得相对于父级的位置。 从文档中获取:获得匹配元素集中第一个元素相对于偏移父元素的当前坐标。

getBoundingClientRect-相对于窗口。 来自文档:Element.getBoundingClientRect()方法返回元素的大小及其相对于视口的位置。

此处找到解决方案:Element's coordinates relative to its parent

相关问题