How do you programmatically sync CSS animation progress to scroll?

This tests scroll-driven APIs beyond legacy scroll listeners. A strong answer names CSS animation-timeline and scroll-timeline declaratively, plus the Web Animations API with ScrollTimeline in JS. A red flag is using only requestAnimationFrame style mutations.
What's really being asked
The interviewer wants to know if you are aware of the CSS scroll-driven animations module and the Web Animations API, or if you still rely on legacy main-thread techniques. At the senior level, they care about performance architecture, compositor-friendly animation, and knowing when to use declarative CSS versus imperative JavaScript. This question separates candidates who read specs from those who only copy Stack Overflow snippets.
The full answer
First, the declarative CSS path: using animation-timeline with the scroll function or a named scroll-timeline to attach keyframes to a scroll container so the browser drives the animation on the compositor without JavaScript. Second, the imperative JS path: using the Web Animations API to create an animation with a ScrollTimeline instance, which lets you control playback rate, pause, or seek programmatically while still keeping the work off the main thread. Third, mention animation-range or animation-range-start and animation-range-end to limit when the animation applies within the scroll span. Fourth, note that view-timeline and ViewTimeline exist for element-based visibility progress, not just container scrolling. Fifth, acknowledge progressive enhancement or feature detection because support is not universal across all browsers.
The mistakes people make
The biggest red flag is describing a requestAnimationFrame loop that reads window scrollY or element scrollTop on every frame and then mutates element style transform or opacity directly. This approach thrashes layout, runs on the main thread, and ignores the modern primitives the browser now provides. Another weak answer is mentioning only CSS transitions and trying to trigger them with class toggles on scroll events, which is still event-driven and not truly synced to scroll progress. Finally, confusing scroll-timeline with the older scroll-behavior property shows a lack of depth.
What usually comes next
The interviewer might ask how you would polyfill or gracefully degrade for browsers without scroll-driven animation support. They could ask about the difference between a scroll progress timeline and a view progress timeline, or when you would choose CSS scroll-timeline over the Web Animations API. They may also probe performance: what happens if you animate layout properties like width or top instead of transform, or how you avoid forced synchronous layout when combining JS and scroll. Another follow-up is how to handle bidirectional scrolling or nested scroll containers with multiple named timelines.
A concrete example
Imagine a product page where a sticky section heading scales down as the user scrolls through a long list. In CSS, you would define scroll-timeline: --section-scroll on the container, then on the heading set animation-timeline: --section-scroll, animation: scale-down linear, and animation-range: contain 0% contain 100%. In JavaScript, you could instead create the same effect by calling heading.animate with a KeyframeEffect that scales from 1 to 0.8, passing a new ScrollTimeline source set to the container and axis set to block. This gives you an animation object you can pause, reverse, or adjust playbackRate based on user interaction while the browser handles the scroll linkage.
Interview question
You need to programmatically pause, seek, and change playback rate of an animation driven by a container's scroll position while keeping the work compositor-only. Which approach fits?
- a.Create the animation with the Web Animations API and assign it a ScrollTimeline instanceCorrect
- b.Define a CSS scroll-timeline and use animation-timeline with animation-range on the element
- c.Use a requestAnimationFrame loop to read scrollTop and mutate element.style.transform each frame
- d.Set scroll-behavior: smooth on the container and trigger CSS transitions via scroll event listeners
Why? this is the answer
The Web Animations API paired with ScrollTimeline gives imperative control over playback rate, pausing, and seeking while the browser runs the animation on the compositor. Option B is declarative CSS, which synchronizes to scroll but does not expose those programmatic controls, and option C thrashes layout on the main thread.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #css
- #scroll-driven-animations
- #web-animations-api
- #performance
- #senior
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles