写了八年滚动动画,今天才发现入口动画从来不需要一行 JS——Chrome 146 用三个 CSS 属性把这件事彻底原生化了
写了八年滚动动画,今天才发现入口动画从来不需要一行 JS——Chrome 146 用三个 CSS 属性把这件事彻底原生化了。
IntersectionObserver 配 class toggle,这套写法从 2015 年用到现在。每个做滚动入场动画的项目都要来一遍:新建 observer、写回调、在回调里调 element.animate() 或写 class、最后 unobserve。11 行 JS,换一个元素的淡入。团队里每来一个新人,都要重新理解这套流程。
Chrome 146 把这件事彻底做完了。
新的写法和旧的写出来是这个对比:
旧方案,11 行:
const io = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.animate([
{ opacity: 0, translate: "0 32px" },
{ opacity: 1, translate: "0 0" }
], { duration: 600, easing: "ease-out", fill: "both" });
io.unobserve(entry.target);
}
});
});
document.querySelectorAll(".reveal").forEach((el) => io.observe(el));
新方案,5 行:
.reveal {
animation: fade-up 0.6s ease-out both;
animation-trigger: --t play-forwards;
timeline-trigger-name: --t;
timeline-trigger-source: view();
}
@keyframes fade-up {
from { opacity: 0; translate: 0 32px; }
to { opacity: 1; translate: 0 0; }
}
没有 JS。没有 observer。没有 unobserve。浏览器自己知道什么时候该触发动画。
scroll-driven 和 scroll-triggered 根本不是一回事
很多人把这两个混起来说,其实解决的是不同问题。
animation-timeline: view() 是 scroll-driven:动画进度和滚动位置完全绑定,你滚多少它播多少,往回滚就倒放。整个动画没有”开始”和”结束”,只有”滚到哪里”。
animation-trigger + timeline-trigger 是 scroll-triggered:滚动位置只决定”什么时候触发”,触发之后动画按自己的 duration 跑完,跟滚不滚动没关系。这才是 IntersectionObserver 那个场景的正确替代品。
| scroll-driven | scroll-triggered | |
|---|---|---|
| 动画进度 | 绑定滚动位置 | 按固定 duration 播放 |
| 滚动停止时 | 动画也停 | 动画继续跑完 |
| 典型场景 | 视差滚动、进度条 | 卡片入场、淡入 |
| 推出时间 | Chrome 115(2023) | Chrome 146(2026) |
三个属性的协作关系
timeline-trigger-name 给这个触发器起个名字,用 dashed-ident,比如 --fade-in。timeline-trigger-source: view() 说监听视口交叉。animation-trigger: --fade-in play-forwards 把动画绑定到这个触发器,play-forwards 是进入视口范围时的动作。
三个动作关键字覆盖大多数场景:
play-forwards:进入时播一遍play-forwards play-backwards:进入时播,离开时倒放play-once:播完就不管了,不管用户再滚进来多少次
如果触发器和动画不在同一个元素上,可以把 timeline-trigger 放在父容器,animation-trigger 放在多个子元素。父容器滚入视口,所有子元素同时触发。
.container {
timeline-trigger-name: --cards;
timeline-trigger-source: view();
}
.card {
animation: card-appear 0.35s ease-in-out both;
animation-trigger: --cards play-forwards;
}
trigger-scope 用来防止名字污染。和 anchor-name 一样,触发器名字是全局的,页面上两个地方同时定义 --fade-in 的话后面的会覆盖前面的。加一层 trigger-scope: --local 可以把名字隔离在子树里。
实战:两个常用模式
卡片依次入场:
.card {
animation: card-appear 0.35s ease-in-out both;
animation-trigger: --card-trigger play-forwards;
timeline-trigger-name: --card-trigger;
timeline-trigger-source: view();
}
@keyframes card-appear {
from { opacity: 0; translate: 0 40px; }
to { opacity: 1; translate: 0 0; }
}
进入播、离开倒放(悬停返回效果):
.feature {
animation: feature-pop 0.4s ease-out both;
animation-trigger: --feature play-forwards play-backwards;
timeline-trigger-name: --feature;
timeline-trigger-source: view();
}
渐进增强写法
目前全球覆盖约 62%,只有 Chrome 146+。其他浏览器看到这个属性会直接忽略,不会 break 页面,但用户看不到动画效果。
最稳妥的写法是先写静态回退值,再用 @supports 覆盖:
.reveal {
opacity: 0;
transform: translateY(32px);
}
@supports (animation-trigger: var(--t)) {
.reveal {
opacity: 1;
transform: none;
animation: fade-up 0.6s ease-out both;
animation-trigger: --t play-forwards;
timeline-trigger-name: --t;
timeline-trigger-source: view();
}
}
无障碍方面,prefers-reduced-motion 对 scroll-triggered 动画同样生效,套一层媒体查询即可:
@media (prefers-reduced-motion: reduce) {
.reveal {
animation: none;
opacity: 1;
}
}
下一步怎么上手
三步走:第一,找一个目前在用 IntersectionObserver 触发入场动画的页面;第二,把 JS 那部分删掉,换成 timeline-trigger-name + animation-trigger;第三,加 @supports 兜底,保证降级浏览器不 break。
如果页面上只有几个元素需要入场动画,现在就可以动手。一个 CSS 属性替换掉一整个 JS 模块,这件事本身就值得mark一下。
评论区
登录后可评论。