GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing developer.mozilla.org — scroll-timeline CSS property - CSS | MDN
|
| Initial value | as each of the properties of the shorthand:
|
|---|---|
| Applies to | scroll containers |
| Inherited | no |
| Computed value | as each of the properties of the shorthand:
|
| Animation type | as each of the properties of the shorthand:
|
scroll-timeline =
[ <'scroll-timeline-name'> <'scroll-timeline-axis'>? ]#
<scroll-timeline-name> =
[ none | <dashed-ident> ]#
<scroll-timeline-axis> =
[ block | inline | x | y ]#
In this example, a scroll timeline named --square-timeline is defined using the scroll-timeline-name property on the #container element.
The timeline name is then applied to the animation on the #square element using animation-timeline: --square-timeline.
We include a container with two child <div> elements.
<div id="container">
<div id="square"></div>
<div id="stretcher"></div>
</div>
We set the container to 300px tall to make it scroll vertically.
Using the scroll-timeline property, we define the container as the source of a scroll timeline named --square-timeline, explicitly setting the default vertical scrollbar as the timeline controller.
#container {
height: 300px;
overflow-y: scroll;
scroll-timeline: --square-timeline y;
/* Firefox supports the non-standard vertical/horizontal syntax */
scroll-timeline: --square-timeline vertical;
position: relative;
}
We provide basic styles for the square. We apply the rotateAnimation using the animation-name property. By default, this would use the time-based document timeline. By setting the animation-timeline property to the --square-timeline timeline named above, we set the square to rotate according to the scroll-based timeline, which progresses as the container scrolls.
#square {
background-color: deeppink;
width: 100px;
height: 100px;
position: absolute;
bottom: 0;
animation: rotateAnimation 1ms linear;
animation-timeline: --square-timeline;
}
We ensure the container overflows by defining a stretcher that is wider than its parent. Without content that overflows the container, there would be no scrollbar, and hence no scroll timeline. We also define a CSS keyframe animation that rotates the elements on which it is applied by one full rotation.
#stretcher {
height: 600px;
background: #dedede;
}
@keyframes rotateAnimation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@layer no-support {
@supports not (scroll-timeline: --square-timeline) {
body::before {
content: "Your browser doesn't support the `scroll-timeline` property.";
background-color: wheat;
display: block;
text-align: center;
padding: 1rem 0;
}
}
}
Scroll the vertical bar to see the square animate as you scroll.
The square animates as you scroll. When using scroll-timeline, the duration of the animation depends on how fast you scroll, not the value of the animation-duration property.
| Specification |
|---|
| Scroll-driven Animations # scroll-timeline-shorthand |