【问题标题】:Difference between $(this).position and getBoundingClientRect()$(this).position 和 getBoundingClientRect() 之间的区别
【发布时间】:2018-12-04 10:37:49
【问题描述】:

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

我不知道他们之间的区别。
当我检查时,这是价值差异。

getBoundingClientRect()

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

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

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

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

    在这里找到的解决方案:Element's coordinates relative to its parent

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2012-08-18
      • 1970-01-01
      • 2017-12-22
      • 2012-03-15
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多