Skip to content
大纲

animation-timeline

动画时间线:是css滚动驱动动画的一个实验性功能属性,需要Chrome Canary 115+版本

驱动动画定义了两种新的的timeline, 可以指定动画在元素滚动时运行,@keyframes的进度也会跟随滚动运行,这里使用的是其中的一种scroll-timeline,表示滚动的距离,0% - 100%。

注意

  1. animation-timeline需要写在animation后面;
  2. 不设置scroll()函数的指定容器,滚动关联的是绑定动画元素的父级元素或者定位的元素,可以用scroll-timeline属性设置相关滚动属性,然后赋值给animation-timeline关联起来。scroll-timeline-name变量名需要按--开头命名
  3. 指定容器必须滚动容器的子元素

示例

实现滚动进度条

jstar0

jstar1

jstar2

jstar3

jstar4

jstar5

jstar6

jstar7

jstar8

jstar9

jstar10

jstar11

jstar12

jstar13

jstar14

jstar15

jstar16

jstar17

jstar18

jstar19

jstar20

jstar21

jstar22

jstar23

jstar24

jstar25

jstar26

jstar27

jstar28

jstar29

jstar30

jstar31

jstar32

jstar33

jstar34

jstar35

jstar36

jstar37

jstar38

jstar39

jstar40

jstar41

jstar42

jstar43

jstar44

jstar45

jstar46

jstar47

jstar48

jstar49

jstar50

jstar51

jstar52

jstar53

jstar54

jstar55

jstar56

jstar57

jstar58

jstar59

jstar60

jstar61

jstar62

jstar63

jstar64

jstar65

jstar66

jstar67

jstar68

jstar69

jstar70

jstar71

jstar72

jstar73

jstar74

jstar75

jstar76

jstar77

jstar78

jstar79

jstar80

jstar81

jstar82

jstar83

jstar84

jstar85

jstar86

jstar87

jstar88

jstar89

jstar90

jstar91

jstar92

jstar93

jstar94

jstar95

jstar96

jstar97

jstar98

jstar99

实现横向滚动进度条

jstar0jstar1jstar2jstar3jstar4jstar5jstar6jstar7jstar8jstar9jstar10jstar11jstar12jstar13jstar14jstar15jstar16jstar17jstar18jstar19jstar20jstar21jstar22jstar23jstar24jstar25jstar26jstar27jstar28jstar29jstar30jstar31jstar32jstar33jstar34jstar35jstar36jstar37jstar38jstar39jstar40jstar41jstar42jstar43jstar44jstar45jstar46jstar47jstar48jstar49jstar50jstar51jstar52jstar53jstar54jstar55jstar56jstar57jstar58jstar59jstar60jstar61jstar62jstar63jstar64jstar65jstar66jstar67jstar68jstar69jstar70jstar71jstar72jstar73jstar74jstar75jstar76jstar77jstar78jstar79jstar80jstar81jstar82jstar83jstar84jstar85jstar86jstar87jstar88jstar89jstar90jstar91jstar92jstar93jstar94jstar95jstar96jstar97jstar98jstar99

实现滚动修改banner图

XP经典

jstar0

jstar1

jstar2

jstar3

jstar4

jstar5

jstar6

jstar7

jstar8

jstar9

jstar10

jstar11

jstar12

jstar13

jstar14

jstar15

jstar16

jstar17

jstar18

jstar19

jstar20

jstar21

jstar22

jstar23

jstar24

jstar25

jstar26

jstar27

jstar28

jstar29

jstar30

jstar31

jstar32

jstar33

jstar34

jstar35

jstar36

jstar37

jstar38

jstar39

jstar40

jstar41

jstar42

jstar43

jstar44

jstar45

jstar46

jstar47

jstar48

jstar49

jstar50

jstar51

jstar52

jstar53

jstar54

jstar55

jstar56

jstar57

jstar58

jstar59

jstar60

jstar61

jstar62

jstar63

jstar64

jstar65

jstar66

jstar67

jstar68

jstar69

jstar70

jstar71

jstar72

jstar73

jstar74

jstar75

jstar76

jstar77

jstar78

jstar79

jstar80

jstar81

jstar82

jstar83

jstar84

jstar85

jstar86

jstar87

jstar88

jstar89

jstar90

jstar91

jstar92

jstar93

jstar94

jstar95

jstar96

jstar97

jstar98

jstar99

代码

vue
<template>
  <div class="animationTimelineWrap">
    <h3>实现滚动进度条</h3>
    <div class="demoWrap">
      <div class="contentWrap">
        <div class="scrollLineWrap"></div>
        <p v-for="item in list" :key="'jstar' + item.id">
          {{ item.text }}
        </p>
      </div>
    </div>
    <h3>实现横向滚动进度条</h3>
    <div class="demoWrap">
      <div class="contentWrap2">
        <div class="scrollLineWrap"></div>
        <span v-for="item in list" :key="'jstar' + item.id" style="margin-right: 10px;">
          {{ item.text }}
        </span>
      </div>
    </div>
    <h3>实现滚动修改banner图</h3>
    <div class="demoWrap2">
      <div class="contentWrap3">
        <div class="imgWrap">
          XP经典
        </div>
        <p v-for="item in list" :key="'jstar' + item.id">
          {{ item.text }}
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
const list = []
for(let i = 0; i < 100; i++) {
  list.push({
    id: i,
    text: 'jstar' + i
  })
}
</script>

<style lang="scss" scoped>
@supports (animation-timeline: scroll()) {
  .animationTimelineWrap {
    @keyframes widthScrollAnim {
      from {
        width: 0%;
      }

      to {
        width: 100%;
      }
    }

    @keyframes widthScrollAnim2 {
      0% {
        height: 300px;
        font-size: 200px;
        color: var(--vp-c-brand);
      }

      6% {
        height: 60px;
        font-size: 28px;
        color: rgb(255 255 255);
        background-color: var(--vp-c-brand);
      }

      100% {
        height: 60px;
        font-size: 28px;
        color: rgb(255 255 255);
        background-color: var(--vp-c-brand);
      }
    }

    font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;

    .demoWrap {
      position: relative;
      height: 300px;
      margin: 0;
      margin-top: 10px;
      overflow: hidden;
      box-shadow: 0 0 2px 2px gainsboro;

      .contentWrap {
        width: 100%;
        height: 100%;
        overflow-y: scroll;
        scroll-timeline-name: --my-scroller;
        scroll-timeline-axis: y;

        .scrollLineWrap {
          position: absolute;
          top: 0;
          left: 0;
          width: 10%;
          height: 8px;
          background-color: var(--vp-c-brand);
          animation: widthScrollAnim 3s linear;
          /* stylelint-disable-next-line property-no-unknown */
          animation-timeline: --my-scroller;
        }
      }

      .contentWrap2 {
        width: 100%;
        height: 100%;
        padding-top: 10px;
        overflow-y: hidden;
        box-sizing: border-box;
        scroll-timeline-name: --my-scroller2;
        scroll-timeline-axis: x;

        .scrollLineWrap {
          position: absolute;
          top: 0;
          left: 0;
          width: 10%;
          height: 8px;
          background-color: var(--vp-c-brand);
          animation: widthScrollAnim 3s linear;
          /* stylelint-disable-next-line property-no-unknown */
          animation-timeline: --my-scroller2;
        }
      }
    }

    .demoWrap2 {
      position: relative;
      height: 600px;
      margin: 0;
      margin-top: 10px;
      overflow: hidden;
      box-shadow: 0 0 2px 2px gainsboro;

      .contentWrap3 {
        width: 100%;
        height: 100%;
        padding-top: 300px;
        overflow-x: hidden;
        box-sizing: border-box;
        scroll-timeline-name: --my-scroller3;
        scroll-timeline-axis: y;

        .imgWrap {
          position: absolute;
          top: 0;
          left: 0;
          display: flex;
          width: 100%;
          height: 300px;
          overflow: hidden;
          font-family: Arial, Helvetica, sans-serif;
          font-weight: bold;
          background-image: url("https://gss0.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/962bd40735fae6cda56d9afa0eb30f2443a70f4b.jpg");
          background-position: 50% 50%;
          background-repeat: no-repeat;
          background-size: cover;
          // background-color: var(--vp-c-yellow);
          animation: widthScrollAnim2 0.1s linear;
          background-blend-mode: soft-light;
          /* stylelint-disable-next-line property-no-unknown */
          animation-timeline: --my-scroller3;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
}
</style>

贡献名单

jstar

jstar

创建者

Released under the MIT License.