research/autoplay-video-only-while-visible

Autoplaying a Background Video Only While It Is on Screen

Engineering Note · November 10, 2026 · 4 minute read · JavaScript · Performance · Accessibility

Observe the video with an IntersectionObserver, call play() when at least half of it is on screen, call pause() when it is not, and skip all of it when the visitor has asked for reduced motion. That works in Chromium and takes about 20 lines. There is one trap. isIntersecting turns true at the first pixel of overlap, so a threshold alone does not wait for half the video. You have to read intersectionRatio yourself.

Everything below was run with Node.js 22.23.4, Playwright 1.63.0, Chromium 153.0.8010.12 (headless) and ffmpeg 6.1.1 on Linux. The outputs are the real ones from this run.

What did we build and test?

The page has a tall spacer, then one 320 by 180 pixel video, then another spacer. The video is a 30-second VP9 test pattern made with ffmpeg:

ffmpeg -f lavfi -i testsrc=size=320x180:rate=15:duration=30 -c:v libvpx-vp9 -b:v 100k clip.webm

We used WebM because the Chromium that ships with Playwright is not guaranteed to decode every format a production site would serve. This note says nothing about H.264 or MP4 playback.

The markup:

<video id="bg" muted loop playsinline preload="metadata" src="clip.webm" data-autoplay></video>

muted and playsinline are the attributes usually recommended for background video. We tested only muted video, so we make no claim about video with sound. The video has no autoplay attribute, because the script decides when it starts.

What is the first version of the script?

const vids = document.querySelectorAll('video[data-autoplay]');
const reduce = matchMedia('(prefers-reduced-motion: reduce)');

function setup() {
  if (reduce.matches) { vids.forEach(v => { v.pause(); v.controls = true; }); return null; }
  const io = new IntersectionObserver(entries => {
    for (const e of entries) {
      const v = e.target;
      if (e.isIntersecting) v.play().catch(() => {});
      else v.pause();
    }
  }, { threshold: [0, 0.5] });
  vids.forEach(v => io.observe(v));
  return io;
}
setup();

The reduced-motion branch returns before any observer exists. The visitor gets a paused video with controls and can press play if they want it.

Does it play and pause as expected?

We drove it with Playwright in two contexts, one with reducedMotion: 'no-preference' and one with 'reduce'. At each step we read paused and currentTime from the video:

[reducedMotion=no-preference]
at top       {"paused":true,"t":0,"controls":false}
scrolled in  {"paused":false,"t":1.2,"controls":false}
scrolled out {"paused":true,"t":1.21,"controls":false} -> 800ms later {"paused":true,"t":1.21,"controls":false}

[reducedMotion=reduce]
at top       {"paused":true,"t":0,"controls":true}
scrolled in  {"paused":true,"t":0,"controls":true}
scrolled out {"paused":true,"t":0,"controls":true} -> 800ms later {"paused":true,"t":0,"controls":true}

With no preference, the video sat at 0 until scrolled into view, ran for about 1.2 seconds, and froze at 1.21 seconds when scrolled away. Eight hundred milliseconds later the time had not moved, so it was paused and not just slowed. With reduced motion, nothing ever played and the controls were on.

Where does the threshold trap show up?

We logged every observer callback. For the 25%-visible case we scrolled so a quarter of the video was in the viewport:

[25% visible]  {"paused":false,"t":0.79,"controls":false} ["out 0.00","in 0.25"]

The video was playing at 25% visible, even though we asked for a 0.5 threshold. The threshold option controls when the callback fires. It does not filter what the callback considers visible. The callback fired at the 0 threshold with a ratio of 0.25, and isIntersecting was true because something overlapped.

The fix is to decide for yourself:

if (e.intersectionRatio >= 0.5) v.play().catch(() => {});
else v.pause();

We reran the 25% case and added a 75% case:

[25% visible]  {"paused":true,"t":0,"controls":false} ["pause 0.00","pause 0.25"]

[75% visible]  {"paused":false,"t":0.8,"controls":false} ["pause 0.00","play 0.75"]

At 25% the video stayed paused. At 75% it played. The two-step log (pause 0.00, then play 0.75) shows the initial callback at registration, followed by the crossing.

What should we remember?

  • Set muted and playsinline, and skip the autoplay attribute so the script is the only thing that starts playback.
  • Check prefers-reduced-motion first and give those visitors controls.
  • Compare intersectionRatio to your own limit. Do not rely on isIntersecting alone.
  • Catch the promise from play(). A browser can refuse it, and an uncaught rejection is noise.
  • Test pause by reading currentTime twice, a moment apart.

Limitations

  • We ran headless Chromium only. Safari and Firefox have their own autoplay policies, and we did not test them.
  • The test video is a small VP9 file. A real hero video in H.264 or AV1 may behave differently, and we did not try one.
  • We did not measure CPU, battery or bandwidth. We checked only that playback state and position behave.
  • We tested a media query that was set when the page loaded. The script does not react if a visitor changes the setting while the page is open.
  • Headless Chromium has no real display, so “on screen” here means inside the emulated viewport.

Frequently asked questions

How do I autoplay a video only when it is visible?

Observe the video element with an IntersectionObserver. Call play() when enough of it is on screen and pause() when it leaves. The video needs the muted and playsinline attributes, because that is the setup we tested. In our test the video was paused at the top of the page, playing after scrolling to it, and paused again after scrolling away.

How do I respect prefers-reduced-motion for background video?

Check matchMedia('(prefers-reduced-motion: reduce)') before setting up the observer. If it matches, leave the video paused and show the native controls so the visitor can choose to play it. In our test with reduced motion emulated, the observer never ran, the video stayed at 0 seconds, and controls were on.

Why does my IntersectionObserver threshold not wait for half the video?

The threshold only decides when the callback runs. The isIntersecting flag is true as soon as any part overlaps. In our test, with thresholds of 0 and 0.5, a video 25% on screen reported isIntersecting true. Check entry.intersectionRatio against your own limit inside the callback.

Does pausing off-screen video save anything?

A paused video stops decoding frames, which is the work we are avoiding. We did not measure CPU, battery or data use in this test, so we cannot give a number. We only verified that the video's paused state and playback position stop changing.

Do I still need a poster image?

Probably, for a video that waits to start, so visitors see something before it plays. We did not test poster behavior here, so treat that as a suggestion. Our test video used preload=metadata and no poster.

More from Research

← All research