在之前的文章《2022年你值得了解的几个CSS新特性(收藏学习)?》中带大家简单介绍了几个CSS新特性,今天带大家深入了解其中的一个新特性(动画杀手锏):@scroll-timeline,希望对大家有所帮助!
|
在之前的文章《2022年你值得了解的几个CSS新特性(收藏学习)》中带大家简单介绍了几个CSS新特性,今天带大家深入了解其中的一个新特性(动画杀手锏):@scroll-timeline,希望对大家有所帮助!
在 CSS 规范 Scroll-linked Animations 中,推出了一个划时代的 CSS 功能。也就是 -- The @scroll-timeline at-rule,直译过来就是滚动时间线。 本文,就将带大家一探究竟,从入门到学会使用 CSS 何为 @scroll-timeline 滚动时间线?什么是
意思是,我们可以定义一个动画效果,该动画的开始和结束可以通过容器的滚动来进行控制。 示意 DEMO再系统性学习语法之前,我们通过一个 DEMO,简单了解一下它的用法: 我们首先实现一个简单的字体 F 旋转动画: <div id="g-box">F</div> #g-box {
animation-name: rotate;
animation-duration: 3s;
animation-direction: alternate;
animation-easing-function: linear;
}
@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}正常而言,它是这样一个简单的动画:
接下来,我们把这个动画和 <div id="g-content">
<div id="g-box">F</div>
</div>#g-content {
width: 300px;
height: 170vh;
background: #999;
}
#g-box {
font-size: 150px;
margin: 70vh auto 0;
animation-name: rotate;
animation-duration: 3s;
animation-direction: alternate;
animation-easing-function: linear;
animation-timeline: box-rotate;
}
@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
@scroll-timeline box-rotate {
source: selector("#g-content");
}这里,我们实现了一个可滚动容器
有意思的来了,我们设置的旋转动画不会自动开始,只有当我们向下滚动的时候,动画才会开始进行,实际效果 Gif:
看到这里,大家应该能够理解 @scroll-timeline 语法介绍接下来,我们先缓一缓,简单看一看 使用 @scroll-timeline moveTimeline {
source: selector("#g-content");
orientation: vertical;
scroll-offsets: 0px, 500px;
}其中:
在设定了一个 @scroll-timeline moveTimeline {
source: selector("#g-content");
orientation: vertical;
scroll-offsets: 0px, 500px;
}
div {
animation-name: move;
animation-duration: 3s;
animation-timeline: moveTimeline;
}
@keyframes move{
0% {
transform: translate(0, 0);
}
100% {
transform: translate(100%, 0);
}
}使用 @scroll-timeline 实现滚动进度指示器之前在 不可思议的纯 CSS 滚动进度条效果 一文中,我们介绍了一种使用渐变实现的纯 CSS 滚动进度指示器效果:
该方法有些小小的瑕疵。其中一个就是当滚动距离太短的时候,进度条右侧会有明显的斜边效果。 而有了 <div id="g-container">
<p>...文本内容...</p>
</div>#g-container {
width: 100vw;
}
#g-container::before {
content: "";
position: fixed;
height: 5px;
left: 0;
top: 0;
right: 0;
background: #ffc107;
animation-name: scale;
animation-duration: 1s;
animation-fill-mode: forwards;
animation-timeline: box-rotate;
transform-origin: 0 50%;
}
@keyframes scale {
0% {
transform: scaleX(0);
}
100% {
transform: scaleX(1);
}
}
@scroll-timeline box-rotate {
source: auto;
orientation: vertical;
}1、我们在页面最上方,通过一个伪元素,实现一个占满屏幕
2、通过设定一个
使用 scroll-offsets 精确控制动画触发时机大家可以再看看上面的 Gif 图,都有一个问题,就是动画的开始时间都是从滚动一开始就开始了,刚好在滚动结束时结束。那么如果我希望动画在滚动的特定阶段触发,那该怎么办呢? 这里,就需要借助 在滚动过程中,我们可以将一个元素,划分为 3 个区域:
在这里,我们就可以得到两个边界,上方边界,下方边界:
而对于上下两个边界,又会有两种状态。以上边界为例子,会有:
对于这两种状态,我们用
这里的 0 和 1 实际表示的是,元素滚动中预期可见的百分比。 有了这些状态值,配合 我们设定一个从左向右并且伴随透明度变化的动画,的看看下面几种情况: 1、滚动动画在元素从下方开始出现时开始,完全出现后截止。 动画运行范围: @keyframes move {
0% {
transform: translate(-100%, 0);
opacity: 0;
}
100% {
transform: translate(0, 0);
opacity: 1;
}
}
@scroll-timeline box-move {
source: auto;
orientation: "vertical";
scroll-offsets:
selector(#g-box) end 0,
selector(#g-box) end 1;
/* Legacy Descriptors Below: */
start: selector(#g-box) end 0;
end: selector(#g-box) end 1;
time-range: 1s;
}
#g-box {
animation-name: move;
animation-duration: 3s;
animation-fill-mode: both;
animation-timeline: box-move;
}效果如下:
2、滚动动画在元素从下方完全出现时开始,在滚动到上方即将离开屏幕后截止: 动画运行范围: // ...
@scroll-timeline box-move {
source: auto;
orientation: "vertical";
scroll-offsets:
selector(#g-box) end 1,
selector(#g-box) start 1;
/* Legacy Descriptors Below: */
start: selector(#g-box) end 1;
end: selector(#g-box) start 1;
time-range: 1s;
}
// ...效果如下:
3、滚动动画在元素滚动到上方即将离开屏幕后开始,完全离开屏幕后截止: 动画运行范围: // ...
@scroll-timeline box-move {
source: auto;
orientation: "vertical";
scroll-offsets:
selector(#g-box) start 1,
selector(#g-box) start 0;
/* Legacy Descriptors Below: */
start: selector(#g-box) start 1;
end: selector(#g-box) start 0;
time-range: 1s;
}
// ...效果如下:
掌握 把上述 3 种情况放在一起,再比较比较:
完整的代码,你可以戳这里:CodePen Demo - @scroll-timeline Demo | element-based offset 使用 @scroll-timeline 实现各类效果在能够掌握 @scroll-timeline 的各个语法之后,我们就可以开始使用它创造各种动画效果了。 譬如如下的,滚动内容不断划入:
甚至可以结合
简而言之,任何动画效果,如今,都可以和滚动相结合起来,甚至乎是配合 SVG 元素也不例外,这里我还简单改造了一下之前的一个 SVG 线条动画:
@scroll-timeline 的实验室特性与特性检测
兼容性如下(2022-03-07):
在最新的 chrome、Edge、Opera 可以通过浏览器配置开启该特性,Chrome 下开启该特性需要:
美酒虽好,但是离完全能用,浏览器大规模支持还需要等待一会,给时间一点时间吧! 特性检测基于目前的兼容性问题,我们可以通过浏览器的特性检测 特性检测的语法也非常简单: @supports (animation-timeline: works) {
@scroll-timeline list-item-1 {
source: selector(#list-view);
start: selector(#list-item-1) end 0;
end: selector(#list-item-1) end 1;
scroll-offsets:
selector(#list-item-1) end 0,
selector(#list-item-1) end 1
;
time-range: 1s;
}
// ...
}通过 最后目前关于 @scroll-timeline 的相关介绍还非常少,但是它确是能够改变 CSS 动画的一个非常大的革新。随着兼容性的逐渐普及,未来势必会在 CSS 中占据一席之地。 本文到此结束,希望对你有帮助 :) (学习视频分享:web前端) 以上就是深入了解CSS动画新特性:@scroll-timeline的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |
