PerformanceElementTiming: intersectionRect 属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

PerformanceElementTiming 接口的只读属性 intersectionRect 返回元素在视口内的矩形区域。

一个 DOMRectReadOnly 对象,表示元素在视口内的矩形区域。

对于显示图像,这是图像在视口内的显示矩形。对于文本,这是节点在视口内的显示矩形。这是包含属于该元素的*所有*文本节点联合的最小矩形。

示例

记录 intersectionRect

在此示例中,通过添加 elementtiming 属性来观察一个 <img> 元素。注册一个 PerformanceObserver 来获取类型为 "element" 的所有性能条目,并使用 buffered 标志来访问观察者创建之前的数据。调用 entry.intersectionRect 将返回一个 DOMRectReadOnly 对象,其中包含图像的显示矩形。

html
<img
  src="image.jpg"
  alt="a nice image"
  elementtiming="big-image"
  id="myImage" />
js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    if (entry.identifier === "big-image") {
      console.log(entry.intersectionRect);
    }
  });
});
observer.observe({ type: "element", buffered: true });

规范

规范
Element Timing API
# dom-performanceelementtiming-intersectionrect

浏览器兼容性