CSS 滚动驱动动画 timeline-scope

timeline-scope

看到 scope 就知道这个属性是和范围有关, 没错, timeline-scope 就是用来修改一个具名时间线(named animation timeline)的范围.

我们介绍过的两种时间线 scroll progress timelineview progress timeline, 使用这两种时间线(通过 view(), scroll(), 或者具名时间线)的元素都需要向上查询 DOM 树找到滚动容器, 这就是时间线的默认范围.

可是有时驱动元素 A 动画可能是 A 的兄弟元素 B 的滚动, 我们应该怎么办呢?

  • A 上使用 animation-timeline 表示需要一个具名时间线来驱动动画, 比如 --whatever-this
  • B 元素上使用 scroll-timeline-name: --whatever-this 表示由 B 元素来提供时间线.
  • 最后, 在 AB 的共同祖先 P 上使用 timeline-scope: --whatever-this 表示时间线范围提升到 PP 的任何后代元素上.

来看例子

📖 我特意没有在两个 div 之间换行, 为了避免换行的出现

html 复制代码
<div class="p">
  <div class="a"></div><div class="b">
    Lorem...
  </div>
</div>
css 复制代码
.p {
  height: 200px;
  timeline-scope: --i-am-here;
}
.p > div {
  width: 50%; /** 为了避免渲染换行符导致宽度不够 */
  height: 100%;
  display: inline-block;
}
.a {
  animation: grow both;
  animation-timeline: --i-am-here;
}
.b {
  overflow: auto;
  scroll-timeline-name: --i-am-here;
}
@keyframes grow {
  from {
    transform: scale(0);
  }
  to {
    transform: scale(1);
  }
}

可以看到效果, 驱动左侧动画的不再是其父元素, 而是其兄弟元素, 从而大大提高了设计使用动画的灵活性.

语法

这个属性的值可以是

  • none: 默认值, 表示时间线范围没有变化.
  • dashed-ident: 和 scroll-timeline-nameview-timeline-name 值相同. 这会导致时间线范围扩大到设置 timeline-scope 的元素和其所有后代.

写下来我们看下 view progress timeline 的效果

html 复制代码
<div class="p">
  <div class="a"></div><div class="b">
    Lorem ...
    <div class="box"></div>
    Lorem ...
  </div>
</div>
css 复制代码
/* 大部分都和前面的一样, 不过我们修改了 .b 并增加了 .box */
.b {
  overflow: auto;
  /* scroll-timeline-name: --i-am-here; */
}
.b .box {
  view-timeline-name: --i-am-here;
}

从下图很容易就看出, 这次驱动左侧动画的是右侧的 .box 元素, 当 .box 开始出现时, 左侧动画进行; 当 .box 完全离开时, 动画结束. 📖注意动画结束时, 右侧的还没有滚动到最后, 说明这次的动画不是由 scroll progress timeline 驱动而是由 view progress timeline 驱动. 这当然显而易见, 因为代码中 scroll progress timeline 的部分被注释了.

📖这里需要再强调 view-timeline-name 需要定义在动画元素上, 而不是滚动容器上.

我们可以让右边的元素也动起来吗? 当然

css 复制代码
.b .box {
  view-timeline-name: --i-am-here;
  animation: grow both;
  animation-timeline: --i-am-here;
}

兼容性

谢谢你看到这里😊

相关推荐
神之王楠8 分钟前
如何通过js加载css和html
javascript·css·html
余生H13 分钟前
前端的全栈混合之路Meteor篇:关于前后端分离及与各框架的对比
前端·javascript·node.js·全栈
程序员-珍16 分钟前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
axihaihai20 分钟前
网站开发的发展(后端路由/前后端分离/前端路由)
前端
流烟默32 分钟前
Vue中watch监听属性的一些应用总结
前端·javascript·vue.js·watch
2401_8572979142 分钟前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
茶卡盐佑星_1 小时前
meta标签作用/SEO优化
前端·javascript·html
Ink1 小时前
从底层看 path.resolve 实现
前端·node.js
金灰1 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
茶卡盐佑星_1 小时前
说说你对es6中promise的理解?
前端·ecmascript·es6