大网数据广告活动
Web 标准与前端开发

LayoutShiftAttribution

· 52CE 我爱测

The `LayoutShiftAttribution` interface provides debugging information about elements which have shifted.

Performance API

The LayoutShiftAttribution interface provides debugging information about elements which have shifted.

Instances of LayoutShiftAttribution are returned in an array by calling LayoutShift.sources.

Instance properties

  • LayoutShiftAttribution.node
    • : Returns the element that has shifted (null if it has been removed).
  • LayoutShiftAttribution.previousRect
    • : Returns a DOMRectReadOnly object representing the position of the element before the shift.
  • LayoutShiftAttribution.currentRect
    • : Returns a DOMRectReadOnly object representing the position of the element after the shift.

Instance methods

  • LayoutShiftAttribution.toJSON()
    • : Returns a JSON-serializable plain object representing the LayoutShiftAttribution object. Automatically called by JSON.stringify().

Examples

The following example observes layout shifts and identifies the most impactful element. The sources array is sorted by impact area, in descending order — so the first element (sources[0]) represents the element that contributed most to the layout shift. For more detail on that, see Debug Web Vitals in the field.

const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (!entry.sources || entry.sources.length === 0) continue;

    const mostImpactfulSource = entry.sources[0];
    console.log("Layout shift score:", entry.value);
    console.log("Most impactful element:", largestShiftSource.node);
    console.log("Previous position:", largestShiftSource.previousRect);
    console.log("Current position:", largestShiftSource.currentRect);
  }
});

observer.observe({ type: "layout-shift", buffered: true });

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。