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
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 |
Yes |
No |
77 |
77 |
No |
Yes |
No |
12.0 |
currentRect |
77 |
80 |
No |
No |
Yes |
No |
77 |
77 |
No |
Yes |
No |
12.0 |
node |
77 |
80 |
No |
No |
Yes |
No |
77 |
77 |
No |
Yes |
No |
12.0 |
previousRect |
77 |
80 |
No |
No |
Yes |
No |
77 |
77 |
No |
Yes |
No |
12.0 |
toJSON |
77 |
80 |
No |
No |
Yes |
No |
77 |
77 |
No |
Yes |
No |
12.0 |
See also
© 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