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

LayoutShift

· 52CE 我爱测

The `LayoutShift` interface of the Performance API provides insights into the layout stability of web pages based on movements of the elements on the page.

Performance API

The LayoutShift interface of the Performance API provides insights into the layout stability of web pages based on movements of the elements on the page.

Instance properties

This interface extends the following PerformanceEntry properties by qualifying them as follows:

  • PerformanceEntry.duration
    • : Always returns 0 (the concept of duration does not apply to layout shifts).
  • PerformanceEntry.entryType
    • : Always returns "layout-shift".
  • PerformanceEntry.name
    • : Always returns an empty string.
  • PerformanceEntry.startTime
    • : Returns a DOMHighResTimeStamp representing the time when the layout shift started.

This interface also supports the following properties:

  • LayoutShift.value
    • : Returns the layout shift score calculated as the impact fraction (fraction of the viewport that was shifted) multiplied by the distance fraction (distance moved as a fraction of viewport).
  • LayoutShift.hadRecentInput
    • : Returns true if LayoutShift.lastInputTime is less than 500 milliseconds in the past.
  • LayoutShift.lastInputTime
    • : Returns the time of the most recent excluding input (user input that would exclude this entry as a contributor to the CLS score) or 0 if no excluding input has occurred.
  • LayoutShift.sources
    • : Returns an array of LayoutShiftAttribution objects with information on the elements that were shifted.

Instance methods

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

Description

A layout shift happens when any element that is visible in the viewport changes its position between two frames. These elements are described as being unstable, indicating a lack of visual stability.

The Layout Instability API provides a way to measure and report on these layout shifts. All tools for debugging layout shifts, including those in the browser's developer tools, use this API. The API can also be used to observe and debug layout shifts by logging the information to the console, to send the data to a server endpoint, or to web page analytics.

Performance tools can use this API to calculate a CLS score.

Examples

Logging layout shift values

The following example shows how to capture layout shifts and log them to the console.

const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    // Count layout shifts without recent user input only
    if (!entry.hadRecentInput) {
      console.log("LayoutShift value:", entry.value);
      if (entry.sources) {
        for (const { node, currentRect, previousRect } of entry.sources)
          console.log("LayoutShift source:", node, {
            currentRect,
            previousRect,
          });
      }
    }
  }
});

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

Specifications

Browser compatibility

See also

  • LayoutShiftAttribution
  • CLS

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

资料来源与适用范围

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

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

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