WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / CSS

view() — Sichtbarkeitsfortschritt eines Elements im Scrollport

`view()` erzeugt für `animation-timeline` eine anonyme Achse, deren Fortschritt der Hauptbox beim Durchlaufen des nächsten Scrollports folgt.

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() erzeugt für animation-timeline eine anonyme Achse, deren Fortschritt der Hauptbox beim Durchlaufen des nächsten Scrollports folgt.

Mit view(block) ergeben Scrollpositionen 20/95/170px die Deckkraft 0/0.5/1; das Objekt ist ViewTimeline, die berechnete Form view(), und die Inset-Form meldet true.

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

Standardachse ist block; verfügbar sind auch inline, x und y. Ein oder zwei Insets wie 10px 20px verkleinern den Sichtbereich; auto übernimmt passendes scroll-padding. Im Cover-Bereich bedeutet 0% das Eintreten der Startkante vom Ende, 100% das Austreten der Endkante am Anfang. Ohne Hauptbox, Scrollport oder Überlauf ist die Zeit null.

Beispiele

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)'));

Ergebnis

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

`view()` erzeugt für `animation-timeline` eine anonyme Achse, deren Fortschritt der Hauptbox beim Durchlaufen des nächsten Scrollports folgt.

Quellen