Background video, image reveals and next-page image warming. No dependencies, no framework, about 4 KB gzipped for the video half.
It doesn't flash, doesn't hog the decoder, doesn't eat data on a metered connection, doesn't ignore reduced motion, and doesn't autoplay without giving anyone a way to stop it.
Install
npm install polite-media
The whole thing
<div class="your-own-box" data-polite-media>
<img src="poster.avif" alt="" />
<video muted loop playsinline preload="none" tabindex="-1" aria-hidden="true">
<source src="hero.mp4" type="video/mp4" />
</video>
</div>
<button type="button" data-polite-pause-control aria-pressed="false">
Pause background video
</button>
import { register } from 'polite-media/video';
import 'polite-media/video.css';
register(document.querySelector('[data-polite-media] video'));
What that bought you
- The poster is swapped on a genuinely presented frame, not on the `playing` event.
- The video is fetched after the page has loaded, so it competes with nothing.
- It stops once it is mostly scrolled past, instead of decoding for the whole page.
- A refused autoplay recovers on the first gesture rather than staying stuck.
- Reduced motion is followed live, and the pause survives a navigation.
- A looping video without a stop control is reported, because WCAG 2.2.2 requires one.
See it break, on purpose
Sixteen pages covering the awkward cases: grids that arbitrate between videos, art-directed posters, codec fallback, save-data, no-JS, and the reveal failsafe.