Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

animation-timeline: select what drives the animation

Keep document time, scroll progress and element visibility distinct when choosing an animation’s timeline.

Version: CSS Modules

Syntax

animation-timeline: auto | none | <dashed-ident> | scroll() | view();

animation-timeline selects the progress source for each CSS animation. auto uses the document timeline, none associates no timeline, and a dashed identifier such as --track refers to a named scroll or view timeline in scope. scroll() follows scrolling on its selected container and axis; view() follows an element’s passage through a scrollport. A keyframes name does not define a timeline of the same name. Multiple comma-separated values match the animation-name list.

A missing named timeline does not silently fall back to the document clock. A scroll timeline also needs a valid scroll source with scrollable overflow on the chosen axis. none is not the same as animation-play-state: paused: selecting no timeline is different from retaining and pausing an existing play position. With scroll-driven animation, duration auto and a finite iteration count can spread the effect across its attachment range.

The example compares an anonymous scroll(nearest block) timeline with --track defined on the same scroller. At offset 400 of an 800px range, both show opacity 0.5. Changing them to none and a missing --missing timeline gives the underlying opacity 0.2 with fill none. A separate paused document-clock animation, sampled at 500ms of a one-second duration, gives 0.5. No login, network request or external clock is involved.

The final check shows that a later animation shorthand resets the chosen scroll timeline to auto. Put timeline and range declarations after that shorthand to preserve them. Test actual behavior in target browsers and provide static content when a timeline is unavailable. The example verifies named and anonymous scroll timelines plus a document-clock control; it does not claim to exercise view() geometry.

Examples

const style=document.createElement('style');style.textContent='@keyframes fade{from{opacity:0}to{opacity:1}}';document.head.append(style);
const scroller=document.createElement('div');scroller.style.cssText='height:200px;width:200px;overflow:auto;scroll-behavior:auto';
const space=document.createElement('div');space.style.height='1000px';
const box=document.createElement('div');box.style.cssText='height:20px;width:20px;opacity:.2;animation:fade auto linear both;animation-timeline:scroll(nearest block)';
space.append(box);scroller.append(space);document.body.append(scroller);
const frame=()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)));
const sample=async positions=>{const values=[];for(const y of positions){scroller.scrollTop=y;await frame();values.push(Number(getComputedStyle(box).opacity).toFixed(2));}return values.join(',')};
scroller.style.scrollTimeline='--track block';box.style.animationFillMode='none';
const named=box.cloneNode();named.style.animationTimeline='--track';space.append(named);
scroller.scrollTop=400;await frame();const halfway=[box,named].map(e=>Number(getComputedStyle(e).opacity).toFixed(2)).join(',');
box.style.animationTimeline='none';named.style.animationTimeline='--missing';await frame();const inactive=[box,named].map(e=>Number(getComputedStyle(e).opacity).toFixed(2)).join(',');
box.style.animation='fade 1s linear both paused';getComputedStyle(box).opacity;const time=box.getAnimations()[0];time.currentTime=500;const clock=Number(getComputedStyle(box).opacity).toFixed(2);
box.style.animationTimeline='scroll(nearest block)';const before=box.style.animationTimeline;box.style.animation='fade 2s linear';const after=getComputedStyle(box).animationTimeline;
console.log('halfwayAnonymousNamed='+halfway+'; inactiveNoneMissing='+inactive+'; clockAt500ms='+clock+'; reset='+before+' / '+after);

Result

halfwayAnonymousNamed=0.50,0.50; inactiveNoneMissing=0.20,0.20; clockAt500ms=0.50; reset=scroll() / auto

Executed in Chromium 157.0.8103.51. The browser serializes scroll(nearest block) as scroll(); those omitted arguments are the defaults, not a lost configuration.

Sources