Sub topic 15
Reacting to skipped state with contentvisibilityautostatechange
This chapter assumes you know content-visibility: auto and contain-intrinsic-size from the earlier chapters, and that you can write basic JavaScript event listeners.
Why you may need to know
With content-visibility: auto, the browser decides when an element's contents are rendered. Usually you don't need to know. Sometimes you do, because the contents may run work that is wasted while the element is skipped. Examples are a running animation, a polling widget, or a video.
The contentvisibilityautostatechange event tells your script when the browser starts or stops skipping the contents of an element.
The two states
The event fires on the element that has content-visibility: auto. It carries one property, event.skipped. The value is true when the contents have just been skipped and false when they are being rendered.
A working example
This page pauses a CSS animation inside each section while the section is skipped.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pause work when skipped</title>
<style>
.chunk {
content-visibility: auto;
contain-intrinsic-size: auto 800px;
height: 800px;
}
.spinner {
width: 40px;
height: 40px;
background: tomato;
animation: spin 2s linear infinite;
}
.chunk.is-paused .spinner {
animation-play-state: paused;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<section class="chunk"><div class="spinner"></div></section>
<section class="chunk"><div class="spinner"></div></section>
<section class="chunk"><div class="spinner"></div></section>
<script>
document.querySelectorAll('.chunk').forEach((chunk) => {
chunk.addEventListener('contentvisibilityautostatechange', (event) => {
chunk.classList.toggle('is-paused', event.skipped);
});
});
</script>
</body>
</html>The script uses toggle with event.skipped as the second argument. The is-paused class is added while the contents are skipped and removed when they render again.
Behavior to remember
- The event fires only for
content-visibility: auto. An element withhiddendoes not fire it, because you control that state yourself. - Rendering can start for reasons other than scrolling. Find-in-page, focus, and text selection inside the element also make the browser render it, so
skippedcan becomefalsewithout a scroll. - The event fires around a rendering update, not at every scroll position. Keep handlers short, and don't do layout-forcing reads such as
getBoundingClientRect()inside them. - Support is not universal. A browser that lacks the event ignores your listener, so the page must still work without it. In the example, the animation keeps running in such a browser, which costs some efficiency but does not break anything.
Common mistakes
- Listening on a child instead of the element with
content-visibility: auto. - Expecting the event on an element that uses
content-visibility: hidden. - Assuming
skipped: falsemeans the element is on screen. It means only that the browser is rendering it.
Exercise
A dashboard widget polls a server every second and sits inside a section with content-visibility: auto. How would you use contentvisibilityautostatechange to avoid wasted polling, and why must the page still work if the event never fires?
Show answer
Add a listener on the section. When event.skipped is true, clear the polling timer. When it is false, start polling again and refresh once to show current data. Some browsers do not support the event, so the widget must keep polling correctly without it. The event only saves work and must not be needed for correctness.