小车平移动画

117 阅读1分钟

效果图

小车动画.gif

实现代码

<!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">
  <!-- 1. 引入vue -->
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <!-- 2. 引入样式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <!-- 3. 引入组件库 -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <style>

  </style>
  <title>小车动画</title>
  <style>
    .box {
      height: 400px;
      width: 1200px;
      border: 2px solid #ccc;
      background-color: pink;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }

    .roadImg {
      position: absolute;
      bottom: 23%;
      left: 25%;
      transform: scale(1.3);
    }

    .carImg {
      position: absolute;
      z-index: 1;
      transform: scale(1.2);
      left: 40%;
      bottom: 31%;
      animation: carMove 2s linear 1.2s forwards;
    }

    .pillarImg {
      z-index: 1;
      position: absolute;
      bottom: 26%;
      left: 23%;
    }

    .baojingGif {
      position: absolute;
      z-index: 1;
      top: 30%;
      transform: scale(0.5);
      left: 20.8%;
    }

    .railImg {
      z-index: 2;
      position: absolute;
      left: 25%;
      top: 55%;
      animation: rail 1s linear forwards;
    }

    .lightImg {
      position: absolute;
      z-index: 2;
      top: 18%;
      left: 16%;
    }

    /* 小车移动动画 */
    @keyframes carMove {
      0% {}

      50% {}

      90% {
        opacity: 1;
      }

      100% {
        /* transform: scale(1.2) translateX(-400px); */
        transform: scale(1.2) translate(-180%, 45%);
        opacity: 0;
      }
    }

    /* 栏杆抬起动画 */
    @keyframes rail {
      0% {
        transform: rotate(0deg);
        transform-origin: 0 0;
      }

      100% {
        transform: rotate(-90deg);
        transform-origin: 0 0;
      }
    }
  </style>
</head>



<body>
  <div id="app">
    <div class="box">

      <img src="img/red.png" alt="" class="lightImg" />

      <img src="img/rail_02.png" class="railImg" alt="" />

      <img src="img/gray_01.png" class="baojingGif" alt="" />

      <img src="img/pillar_01.png" alt="" class="pillarImg" />

      <img src="img/car.png" alt="" class="carImg car-stop">

      <img src="img/road.png" alt="" class="roadImg" />
    </div>
  </div>
</body>

<script>
  new Vue({
    el: '#app',
    data () {
      return {
      }
    },
  })
</script>


</html>

注意点

使用z-index属性让小车在路的上层