LayoutShiftAttribution
The LayoutShiftAttribution
interface of the Layout Instability API provides debugging information about elements which have shifted.
Instances of LayoutShiftAttribution
are returned in an array by calling LayoutShift.sources
.
Properties
LayoutShiftAttribution.Node
Read only-
Returns the element that has shifted (null if it has been removed).
LayoutShiftAttribution.previousRect
Read only-
Returns a
DOMRectReadOnly
object representing the position of the element before the shift. LayoutShiftAttribution.currentRect
Read only-
Returns a
DOMRectReadOnly
object representing the position of the element after the shift.
Methods
LayoutShiftAttribution.toJSON()
-
Returns a JSON representation of the
LayoutShiftAttribution
object.
Examples
The following example finds the element that is causing the largest layout shift, and prints that node
to the console. For more detail on this see Debug Web Vitals in the field.
function getCLSDebugTarget(entries) {
const largestEntry = entries.reduce((a, b) => {
return a && a.value > b.value ? a : b;
});
if (largestEntry && largestEntry.sources && largestEntry.sources.length) {
const largestSource = largestEntry.sources.reduce((a, b) => {
return a.node && a.previousRect.width * a.previousRect.height >
b.previousRect.width * b.previousRect.height ? a : b;
});
if (largestSource) {
return largestSource.node;
}
}
}
Specifications
Specification |
---|
Layout Instability # sec-layout-shift-attribution |
Browser compatibility
BCD tables only load in the browser