animation初体验

121 阅读1分钟

旋转的星球主要通过rotate()旋转函数来实现。实际上,蓝色的地球和黑色的月球并没有发生旋转,只是其父级旋转形成的视觉上的旋转效果

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Moon</title>
  <style>
    .box {
      width: 300px;
      height: 300px;
      padding: 1px;
      position: relative;
      transform: scale(0.5);
    }

    .sunline {
      display: flex;
      position: relative;
      width: 400px;
      height: 400px;
      border: 2px solid black;
      border-radius: 50%;
      animation: cxx 10s infinite linear;
    }

    .sun {
      width: 100px;
      height: 100px;
      background-color: red;
      margin: auto;
      border-radius: 50%;
    }

    @keyframes cxx {
      100% {
        transform: rotate(360deg);
      }
    }

    .earthline {
      display: flex;
      width: 200px;
      border: 2px solid black;
      height: 200px;
      border-radius: 50%;
      position: absolute;
      top: 50%;
      right: 0;
      margin: -100px -100px 0 0;
      animation: cxx 2s infinite linear;
    }

    .earth {
      margin: auto;
      width: 50px;
      height: 50px;
      background-color: rgb(46, 174, 224);
      border-radius: 50%;
    }

    .moon {
      position: absolute;
      left: 0;
      top: 50%;
      width: 20px;
      height: 20px;
      background-color: rgb(82, 77, 77);
      border-radius: 50%;
      margin: -10px 0 0 -10px;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="sunline">
      <div class="sun"></div>
      <div class="earthline">
        <div class="earth"></div>
        <div class="moon"></div>
      </div>
    </div>
  </div>
</body>

</html>```