Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

animation-range: choose the portion of a scroll timeline

Attach the same keyframes to a selected interval of scrolling, rather than adding a delay measured in seconds.

Version: CSS Modules

Syntax

animation-range: normal | <start> <end>?; /* comma-separated per animation */

animation-range is the shorthand for animation-range-start and animation-range-end. It sets where an animation is attached on its timeline, not the element’s scroll limits. With a scroll-driven animation, percentages without a named range refer to the whole timeline. Values such as entry or exit refer instead to named portions of a view timeline; they are not scroll-container selectors. Multiple comma-separated ranges correspond to the animation-name list.

In the example, a 200px viewport contains 1000px of content, so the scrollable distance is 800px. The range 25% 75% attaches a single linear fade to offsets 200px through 600px. Reads at 0,200,400,600,800 give opacity 0,0,0.5,1,1. Scrolling back to 400 restores 0.5: scrolling drives the progress in either direction. animation-fill-mode: both preserves endpoint values outside that interval; the range alone does not request that behavior.

Omitting the end has two distinct meanings. A lone 25% sets the start to 25% and resets the end to normal. A lone entry uses the beginning and end of the entry range; its two longhands may serialize simply as entry even though their default offsets differ. The example checks these parsed longhands but does not measure view-timeline entry geometry. A seconds pair such as 2s 4s is rejected.

Write animation-timeline and animation-range after the animation shorthand, because that shorthand resets them. Use a usable static fallback and test target browsers: this is scroll-driven animation behavior, not a portable replacement for animation-delay on a document clock. The example measures actual opacity after rendering frames, without network resources or user data.

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(',')};
box.style.animationRange='25% 75%';
const values=await sample([0,200,400,600,800,400]);
const shorthand=document.createElement('div').style;shorthand.animationRange='entry';const named=shorthand.animationRangeStart+' / '+shorthand.animationRangeEnd;
shorthand.animationRange='25%';const lone=shorthand.animationRangeStart+' / '+shorthand.animationRangeEnd;
console.log('scroll-range='+(scroller.scrollHeight-scroller.clientHeight)+'; opacity='+values+'; entry='+named+'; lone='+lone+'; seconds='+CSS.supports('animation-range','2s 4s'));

Result

scroll-range=800; opacity=0.00,0.00,0.50,1.00,1.00,0.50; entry=entry / entry; lone=25% / normal; seconds=false

Executed in Chromium 157.0.8103.51. The fade uses duration auto, one iteration and linear easing; different iteration, easing or fill settings change the visible result.

Sources