Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

view() — subject visibility progress through a scrollport

`view()` creates an anonymous timeline for `animation-timeline` whose progress follows the subject’s principal box crossing its nearest scrollport.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

view( [ <axis> || <'view-timeline-inset'> ]? )

view() creates an anonymous timeline for animation-timeline whose progress follows the subject’s principal box crossing its nearest scrollport.

With view(block), scroll positions 20/95/170px produce opacity 0/0.5/1; the object is a ViewTimeline, computed notation is view(), and the inset form reports support true.

Grammar: view( [ <axis> || <'view-timeline-inset'> ]? ).

The default axis is block; inline, x, and y are also available. One or two inset values such as 10px 20px shrink the visibility range; auto takes the corresponding scroll-padding. In the cover range, 0% is the start edge entering from the scrollport end and 100% is the end edge leaving at the start. No principal box, scrollport, or overflow makes current time null.

Examples

const style=document.createElement('style');style.textContent='@keyframes reveal{from{opacity:0}to{opacity:1}}.scroller{width:100px;height:100px;overflow:auto}.track{height:320px;padding-top:120px;box-sizing:border-box}.subject{height:50px;animation:reveal linear both;animation-timeline:view(block)}';document.head.append(style);
const scroller=document.createElement('div');scroller.className='scroller';const track=document.createElement('div');track.className='track';const subject=document.createElement('div');subject.className='subject';track.append(subject);scroller.append(track);document.body.append(scroller);
const settle=()=>new Promise(resolve=>requestAnimationFrame(()=>requestAnimationFrame(resolve)));await settle();
const animation=subject.getAnimations()[0];const sample=async top=>{scroller.scrollTop=top;await settle();return getComputedStyle(subject).opacity};
const enter=await sample(20),middle=await sample(95),exit=await sample(170);
console.log('opacity='+enter+'/'+middle+'/'+exit+'; timeline='+animation.timeline.constructor.name+'; inset='+getComputedStyle(subject).animationTimeline+'; supported='+CSS.supports('animation-timeline','view(block 10px 20px)'));

Result

opacity=0/0.5/1; timeline=ViewTimeline; inset=view(); supported=true

`view()` creates an anonymous timeline for `animation-timeline` whose progress follows the subject’s principal box crossing its nearest scrollport.

Sources