LayoutShift: hadRecentInput 屬性

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

LayoutShift 介面的只讀屬性 hadRecentInput 返回一個布林值,如果 lastInputTime 的時間戳距離現在小於 500 毫秒,則返回 true

佈局偏移只有在使用者未預期到時才會成為問題,因此由使用者互動(例如使用者展開 UI 元素)引起的佈局偏移通常不被計入佈局偏移指標。hadRecentInput 屬性允許你排除這些偏移。

如果 lastInputTime 的時間戳距離現在小於 500 毫秒,則返回 true,否則返回 false

示例

忽略最近使用者輸入對佈局偏移得分的影響

以下示例顯示瞭如何使用 hadRecentInput 屬性僅統計沒有近期使用者輸入的佈局偏移。

js
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 });

規範

規範
佈局不穩定 API
# dom-layoutshift-hadrecentinput

瀏覽器相容性

另見