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
.
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
.
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.
LayoutShiftAttribution.toJSON()
Returns a JSON representation of the LayoutShiftAttribution
object.
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; } } }
Specification |
---|
Layout Instability # sec-layout-shift-attribution |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
LayoutShiftAttribution |
77
|
80
|
No
|
No
|
64
|
No
|
77
|
77
|
No
|
55
|
No
|
12.0
|
currentRect |
77
|
80
|
No
|
No
|
64
|
No
|
77
|
77
|
No
|
55
|
No
|
12.0
|
node |
77
|
80
|
No
|
No
|
64
|
No
|
77
|
77
|
No
|
55
|
No
|
12.0
|
previousRect |
77
|
80
|
No
|
No
|
64
|
No
|
77
|
77
|
No
|
55
|
No
|
12.0
|
toJSON |
77
|
80
|
No
|
No
|
64
|
No
|
77
|
77
|
No
|
55
|
No
|
12.0
|
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/LayoutShiftAttribution