jQuery offset().left 与 offsetLeft

jQuery offset().left vs offsetLeft

目前我正在尝试通过从 jQuery 重构到 纯 JS.[=13= 来加速我的代码]

我知道还有一些关于此的其他线程,但我找不到任何与我的问题相同的线程。

现在我想用 offsetLeft 获得相同的值,而不是使用 offset().left

有谁知道如何计算正确值的简单说明?

您可以使用 getBoundingClientRect 函数获取此信息:

console.log(document.getElementById('h1').getBoundingClientRect().left);
<div>
  A
</div>
<h1 id="h1">Test</h1>

However keep in mind that jquery is pretty optimized, and the code there is probably using exactly the same function, or probably something pretty close.