Demo01-交错动画

204 阅读1分钟

本例较为简单,主要动画延迟的运用

不过需要注意一下几个问题

  • 动画的延迟仅在animationstart前作用,在animationiteration事件前后不起作用
  • 动画帧的0%和100%省略时会默认为初始状态,可能不是我们想要的结果

代码如下:

html结构
<div class="loading">
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
  <div class="dot"></div>
</div>
样式
  body {
    display: flex;
    height: 100vh;
    justify-content: center;
    align-items: center;
    background: #222;
  }

  .loading {
    display: flex;
  }
  .loading .dot {
    position: relative;
    width: 2em;
    height: 2em;
    margin: 0.8em;
    border-radius: 50%;
  }
  .loading .dot::before {
    position: absolute;
    content: "";
    width: 100%;
    height: 100%;
    background: inherit;
    border-radius: inherit;
    animation: wave 2s ease-out infinite;
  }
  .loading .dot:nth-child(1) {
    background: #7ef9ff;
  }
  .loading .dot:nth-child(1)::before {
    /*动画延时,只在animationstart前有作用,在animationiterration不起效果,下同*/
    animation-delay: 0.2s;
  }
  .loading .dot:nth-child(2) {
    background: #89cff0;
  }
  .loading .dot:nth-child(2)::before {
    animation-delay: 0.4s;
  }
  .loading .dot:nth-child(3) {
    background: #4682b4;
  }
  .loading .dot:nth-child(3)::before {
    animation-delay: 0.6s;
  }
  .loading .dot:nth-child(4) {
    background: #0f52ba;
  }
  .loading .dot:nth-child(4)::before {
    animation-delay: 0.8s;
  }
  .loading .dot:nth-child(5) {
    background: #000080;
  }
  .loading .dot:nth-child(5)::before {
    animation-delay: 1s;
  }

  @keyframes wave {
    50%, 100% {/*如果省略100%,则在100%状态时会使用开始的状态,下同*/
      transform: scale(2.5);
    }
    80%, 100% {
      opacity: 0;
    }
  }
动画演示
动图加载中...