View Transitions & Playing Video
View Transitions & Playing Video êŽë š
I was runninâ my mouth the other day in a conversation about View Transitions and I believe I said that you can keep audio & video playing during a View Transition. Now that Iâm sitting down to actually prove it, the answer seems to be more nuanced:
- Same-Page View Transitions: Just Worksâą
- Multi-Page View Transitions: Doesnât Work⊠well⊠you can fake it though.
Letâs start with that first one as itâs easy and satisfying. But first hereâs both in a demo (chriscoyier). I would embed the demo here, but I find that multi-page view transitions donât behave well in the embed for whatever reason. So rather than you try it and be confused or disappointed, just go directly to that demo link above.
Same-Page View Transitions and Video
If you have a <video> element on the page and you use a document.startViewTransition that manipulates it somehow in the callback, it will preserve the state of the video during the transition. If the video is playing, that playing state will be preserved the entire time. Much like the newangled .moveBefore().
doViewTransition.addEventListener("click", () => {
document.startViewTransition(() => {
const $video = document.querySelector("video");
$video.classList.toggle("fancy");
});
});
Basically nothing to it. This works just as well on an <audio> or <iframe>.
Multi-Page View Transitions and Video
The brass tacks here are that when a page unloads and a new page loads, no state at all is maintained. Even if the exact same <video> is on the next page, itâs not going to remember thatâs playing or where you were.
I was just wrong when I was thinking there was some way to get this to work. There are some understandable sources for the confusion, though. If someone happened to be using an framework that provides SPA (single page app) navigations, you might see persisting video just because, well, the page never unloads. Also: Astro is a popular framework that specifically implemented persistent transitions for video, and does so by essentially forcing an SPA experience with a same-page view transition.
This GitHub thread (w3c/csswg-drafts) is a feature request for multi-page view transitions to be able to keep state and gets into this a little. Bramus notes that this isnât a view transitions specific feature, itâs a more general need for state-saving through page navigations (whatwg/html). It also links to this demo, which⊠makes it work! This is the âfaking itâ I referred to. It doesnât prevent the <video> from being unloaded and re-loaded, it just keeps the state in sessionStorage. So there is a little blip between pages. But hey itâs pretty close!
Hereâs the important bitsâŠ
/* Enable Multi-Page View Transitions */
@view-transition {
navigation: auto;
}
/* Ensure video has a unique name shared on both pages */
video {
view-transition-name: video;
}
<!-- regular link goes between pages -->
<a href="./another-page.html">
Go to Another Page (Multi-Page View Transition)
</a>
<!-- video exists on both pages -->
<video src="https://assets.codepen.io/3/mov_bbb.mp4" controls></video>
window.addEventListener("pageswap", async (e) => {
if (e.viewTransition) {
// page is leaving... save the video state
}
});
window.addEventListener("pagereveal", async (e) => {
if (e.viewTransition) {
// page is entering, get video state and restore
}
});
Hereâs a video where Iâm navigating pages in the demo (chriscoyier) and you can see it working, and Iâve recorded the system audio along with it so you can hear the âblipâ happen between pages:
Again, this demo (chriscoyier) is adapted from this demo from the Chrome gang. Iâm posting mine because I was learning about it and playing with it and hope to make this all more findable information.
Weâve only looked at <video> specifically here, but if you were OK with the âblipâ thing and wanted to do this with, say, a YouTube video that embeds as an <iframe>, the techniques would be the same, youâd just need to dig out the video information with the Iframe Player API in order to save and retrieve the playing video information. I think that makes pretty fun homework if you ask me. đ