HTMLElement: offsetTop 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

HTMLElement 接口的只读属性 offsetTop 返回当前元素的外部边框(包括其外边距)到 offsetParent 的顶部内边距边缘的距离,即最近的已定位的祖先元素。

一个数字。

示例

js
const d = document.getElementById("div1");
const topPos = d.offsetTop;

if (topPos > 10) {
  // object offset is more
  // than 10 pixels from its parent
}

规范

规范
CSSOM 视图模块
# dom-htmlelement-offsettop

浏览器兼容性

另见