ViewTimeline
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
TheViewTimeline interface of theWeb Animations API represents a view progress timeline (seeCSS scroll-driven animations for more details).
Pass aViewTimeline instance to theAnimation() constructor or theanimate() method to specify it as the timeline that will control the progress of the animation.
In this article
Constructor
ViewTimeline()Creates a new
ViewTimelineobject instance.
Instance properties
This interface also inherits the properties of its parent,ScrollTimeline.
subjectRead onlyReturns a reference to the subject element whose visibility within its nearest ancestor scrollable element (scroller) is driving the progress of the timeline and therefore the animation.
startOffsetRead onlyReturns a
CSSNumericValuerepresenting the starting (0% progress) scroll position of the timeline as an offset from the start of the overflowing section of content in the scroller.endOffsetRead onlyReturns a
CSSNumericValuerepresenting the ending (100% progress) scroll position of the timeline as an offset from the start of the overflowing section of content in the scroller.
Instance methods
This interface inherits the methods of its parent,ScrollTimeline.
Examples
>Displaying the subject and offsets of a view progress timeline
In this example, we animate an element with aclass ofsubject along a view progress timeline — it animates when moved upwards through the document as it scrolls. We also output thesubject,startOffset, andendOffset values to an output element in the top-right corner.
HTML
The HTML for the example is shown below.
<div> <h1>Content</h1> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Risus quis varius quam quisque id. Et ligula ullamcorper malesuada proin libero nunc consequat interdum varius. Elit ullamcorper dignissim cras tincidunt lobortis feugiat vivamus at augue. </p> <p> Dolor sed viverra ipsum nunc aliquet. Sed risus pretium quam vulputate dignissim. Tortor aliquam nulla facilisi cras. A erat nam at lectus urna duis convallis convallis. Nibh ipsum consequat nisl vel pretium lectus. Sagittis aliquam malesuada bibendum arcu vitae elementum. Malesuada bibendum arcu vitae elementum curabitur vitae nunc sed velit. </p> <div></div> <p> Adipiscing enim eu turpis egestas pretium aenean pharetra magna ac. Arcu cursus vitae congue mauris rhoncus aenean vel. Sit amet cursus sit amet dictum. Augue neque gravida in fermentum et. Gravida rutrum quisque non tellus orci ac auctor augue mauris. Risus quis varius quam quisque id diam vel quam elementum. Nibh praesent tristique magna sit amet purus gravida quis. Duis ultricies lacus sed turpis tincidunt id aliquet. In egestas erat imperdiet sed euismod nisi. Eget egestas purus viverra accumsan in nisl nisi scelerisque. Netus et malesuada fames ac. </p> <div></div></div>CSS
The CSS for the example looks like this:
.subject { width: 300px; height: 200px; margin: 0 auto; background-color: deeppink;}.content { width: 75%; max-width: 800px; margin: 0 auto;}.output { position: fixed; top: 5px; right: 5px;}p,h1,div { font-family: "Helvetica", "Arial", sans-serif;}h1 { font-size: 3rem;}p { font-size: 1.5rem; line-height: 1.5;}JavaScript
In the JavaScript, we grab references to thesubject andoutput<div>s, then create a newViewTimeline, associating it with thesubject element to specify that the timeline progress is based on this element's visibility through its scrolling ancestor, setting ablock axis, and settinginset values to adjust the position of the box in which the subject is deemed to be visible.
We then animate thesubject element with the Web Animations API. Last of all, we display thesubject,startOffset, andendOffset values in theoutput element.
const subject = document.querySelector(".subject");const output = document.querySelector(".output");const timeline = new ViewTimeline({ subject, axis: "block", inset: [CSS.px("200"), CSS.px("300")],});subject.animate( { opacity: [0, 1], transform: ["scaleX(0)", "scaleX(1)"], }, { fill: "both", timeline, },);output.textContent += `Subject element: ${timeline.subject.nodeName}, `;output.textContent += `start offset: ${timeline.startOffset}, `;output.textContent += `end offset: ${timeline.endOffset}.`;Result
Scroll to see the subject element being animated.
Specifications
| Specification |
|---|
| Scroll-driven Animations> # viewtimeline-interface> |