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 |
瀏覽器相容性
載入中…