scroll-timeline-axis
scroll-timeline-axis CSS 属性用于指定滚动条方向,该方向将用于为命名滚动进度时间线动画提供时间线,此动画通过滚动可滚动元素(滚动器)在顶部和底部(或左侧和右侧)之间进行。scroll-timeline 设置在提供时间线的滚动器上。有关详细信息,请参阅CSS 滚动驱动动画。
注意:如果滚动器元素在其轴维度上未溢出其容器,或者溢出被隐藏或剪裁,则不会创建滚动进度时间线。
scroll-timeline-axis 和 scroll-timeline-name 属性也可以使用 scroll-timeline 简写属性进行设置。
语法
/* Logical property values */
scroll-timeline-axis: block;
scroll-timeline-axis: inline;
/* Non-logical property values */
scroll-timeline-axis: y;
scroll-timeline-axis: x;
值
scroll-timeline-axis 允许的值有
正式定义
正式语法
scroll-timeline-axis =
[ block | inline | x | y ]#
示例
定义滚动进度时间线的轴
在此示例中,使用 scroll-timeline-name 属性在 :root 元素(<html>)上定义了一个名为 --my-scroller 的滚动进度时间线。然后,使用 animation-timeline: --my-scroller 将此时间线应用于具有 animation 类的元素上的动画。
为了演示 scroll-timeline-axis 的效果,此示例使用水平(非默认)滚动条来驱动动画。
HTML
示例的 HTML 如下所示。
<body>
<div class="content"></div>
<div class="box animation"></div>
</body>
CSS
容器的 CSS 将 :root 设置为名为 --my-scroller 的滚动进度时间线的来源,使用 scroll-timeline-name 属性。滚动轴使用 scroll-timeline-axis: x; (Chromium) 和 scroll-timeline-axis: horizontal; (Firefox) 进行设置 — 这导致水平滚动条位置决定动画时间线。
.content 元素的宽度设置为一个较大的值,以使其溢出 :root 元素。
另外值得注意的是,.animation 元素已应用时间线 animation-timeline: --my-scroller;,并且还应用了 animation-duration,以便该示例在 Firefox 中也能正常工作。
:root {
scroll-timeline-name: --my-scroller;
/* Chromium supports the new x/y syntax */
scroll-timeline-axis: x;
/* Firefox still supports the old horizontal/vertical syntax */
scroll-timeline-axis: horizontal;
}
body {
margin: 0;
overflow-y: hidden;
}
.content {
height: 100vh;
width: 2000px;
}
.box {
width: 100px;
height: 100px;
border-radius: 10px;
background-color: rebeccapurple;
position: fixed;
top: 25px;
left: 25px;
}
.animation {
animation: rotate-appear;
animation-timeline: --my-scroller;
animation-duration: 1ms; /* Firefox requires this to apply the animation */
}
@keyframes rotate-appear {
from {
rotate: 0deg;
top: 0%;
}
to {
rotate: 720deg;
top: 100%;
}
}
结果
滚动底部的水平滚动条,查看方块随滚动而动画。
规范
| 规范 |
|---|
| 滚动驱动动画 # propdef-scroll-timeline-axis |
浏览器兼容性
加载中…