如何将多个动画应用于一个元素上,今天让我来告诉你

场景:一个三角形从透明到实体,持续一秒,然后匀速前进五秒,然后变大持续两秒


第一种:CSS + JS

这一种比较麻烦,而且不好控制,写起来也费劲。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 0px;
      height: 0px;
      background: none;
      border: 50px solid red;
      border-color: red transparent transparent transparent;
    }

    .animation1 {
      animation: run 1s;
    }

    @keyframes run {
      0% {
        opacity: 0;
      }

      100% {
        opacity: 1;
      }
    }

    .animation2 {
      animation: run2 5s ease;
      animation-fill-mode: forwards;
    }

    @keyframes run2 {
      0% {
        transform: translateX(0px);
      }

      100% {
        transform: translateX(100px);
        
      }
    }

    .animation3 {
      animation: run3 2s forwards;
    }

    @keyframes run3 {
      0% {
        width: 0px;
        height: 0px;
        background: none;
        border: 50px solid red;
        border-color: red transparent transparent transparent;
      }

      100% {
        width: 0px;
        height: 0px;
        background: none;
        border: 100px solid red;
        border-color: red transparent transparent transparent;
        transform: translateX(100px);
      }
    }
  </style>
</head>

<body>
  <div class="box animation1"></div>
  <script>
    let animationDiv = document.querySelector('.box');
    animationDiv.addEventListener('webkitAnimationEnd', function () {
      animationDiv.classList.remove('.animation1')
      animationDiv.classList.add('animation2')
      setTimeout(() => {
        animationDiv.classList.add('animation3')
      }, 5000);
    })
  </script>
</body>

</html>

在这里插入图片描述

第二种:纯 CSS

animation 不止可以写一个动画,还可以同时使用多个类名

animation: run1, run2, run3;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 0px;
      height: 0px;
      background: none;
      border: 50px solid red;
      border-color: red transparent transparent transparent;
      animation: run 1s ease, run2 5s ease 1s forwards, run3 2s ease 6s forwards;
    }

    @keyframes run {
      0% {
        opacity: 0;
      }

      100% {
        opacity: 1;
      }
    }

    @keyframes run2 {
      0% {
        transform: translateX(0px);
      }

      100% {
        transform: translateX(100px);
      }
    }

    @keyframes run3 {
      0% {
        width: 0px;
        height: 0px;
        background: none;
        border: 50px solid red;
        border-color: red transparent transparent transparent;
      }

      100% {
        width: 0px;
        height: 0px;
        background: none;
        border: 100px solid red;
        border-color: red transparent transparent transparent;
        transform: translateX(100px);
      }
    }
  </style>
</head>

<body>
  <div class="box"></div>
</body>

</html>

在这里插入图片描述

第二种明显比第一种更加顺滑,流畅,写起来也更舒服。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值