CSS3 animation

192 阅读1分钟
  • animation 基础属性详解
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 250px;
      height: 250px;
      margin: 100px;
      background-color: green;

      /* 调用动画 */
      /* 如果设置反向动画 alternate 每一次的来跟回都会算作一次运行动画 */
      /* animation: 动画名称 持续时间 执行次数(infinite: 无限次) 是否反向(alternate: 反向) 运行曲线(linear: 线性曲线) 延迟执行时间; */
      /* animation: move 2s infinite alternate linear 1s; */

      /* animation 属性详解 */
      /* 动画名称 */
      animation-name: move;
      /* 一次动画持续时间 前两个属性是必须的,且顺序固定*/
      animation-duration: 2s;
      /* 动画执行次数 infinite: 无限次 */
      animation-iteration-count: 3;
      /* 动画的方向 normal: 正常 alternate:反向 ... */
      animation-direction: alternate;
      /* 动画延迟执行 */
      animation-delay: 1s;
      /* 设置动画结束后子盒子的状态 forwards: 保持动画结束后的状态位置  backwards: 保持动画开始前的状态,动画完成之后回到动画开始前的状态 */
      animation-fill-mode: forwards;
      /* 运行曲线 linear: 线性曲线  ease-in-out: 先加速后减速  ease-in: 先减速后加速  steps(number): 将当前动画分为指定帧数完成 */
      animation-timing-function: ease-in;
      /* animation-timing-function: steps(3); */
      /* 控制动画执行状态 paused:暂停 running: 播放 */
      animation-play-state: running; 
    }
    @keyframes move {
      0% {

      }
      100% {
        /* transform: translateX(500px) rotate(555deg); */
        transform: translateX(500px) rotate(345deg);
      }
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
</html>

  • animation 基础使用
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
      margin: 100px auto;
      background-color: green;

      /* 调用动画 */
      /* animation: move 2s 3; */
      /* 如果设置反向动画 alternate 每一次的来跟回都会算作一次运行动画 */
      /* animation: 动画名称 持续时间 执行次数(infinite: 无限次) 是否反向(alternate: 反向) 运行曲线(linear: 线性曲线) 延迟执行时间; */
      /* animation: move 2s infinite alternate linear 1s; */
      animation: move 2s infinite alternate linear;
    }

    /* css3中的动画使用:

      类似js中的函数:
        ---先定义 ---在调用

      js 定义函数: 
        function gun(){ 函数体 }

      css3中的动画: 
        定义动画:
        @keyframes 动画名 {
          from { 初始化状态 }
          to { 结束状态 }
        }

        调用:基本语法格式
        animation: 动画名称 持续时间 执行次数(infinite: 无限次) 是否反向(alternate: 反向) 运行曲线(linear: 线性曲线) 延迟执行时间;
    */
    /* 定义动画 */
    @keyframes move {
      from {
        transform: translateX(0px) rotate(0deg);
        /* 动画状态先后顺序是有效果影响 */
        /* transform: rotate(0deg) translateX(0px); */
      }
      to {
        transform: translateX(500px) rotate(-555deg);
        /* 动画状态先后顺序是有效果影响 */
        /* transform: rotate(-555deg) translateX(500px); */
      }
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
</html>
  • 基础使用效果:


  • animation 多组动画
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 200px;
      height: 200px;
      margin: 100px auto;
      background-color: green;
      animation: gun 2s;
    }

    /* 定义多组动画 */
    @keyframes gun {
      0% {
        transform: translateX(0px) translateY(0px);
        background-color: green;
        border-radius: 0;
      }
      25% {
        transform: translateX(500px) translateY(0px);
      }
      50% {
        transform: translateX(500px) translateY(300px);
        border-radius: 50%;
      }
      75% {
        transform: translateX(0px) translateY(300px);
      }
      100% {
        transform: translateX(0px) translateY(0px);
        background-color: red;
        border-radius: 0;
      }
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
</html>
  • 多组动画效果:


  • animation-timing-function: steps(number)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 3px;
      height: 200px;
      background-color: #000;
      margin: 100px auto;
      transform-origin: bottom;
      animation: run 60s infinite steps(60);
    }
    @keyframes run {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
</html>
  • 指定帧数效果: