CSS3 animation 太阳系动画

131 阅读1分钟
<!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>
    body {
      background-color: #000;
      /* 当在缩放窗口之后body高度或者宽度不够之后,运行动画的标签会变大变小,可以使用超出隐藏来避免 */
      overflow: hidden;
    }
    .sun {
      width: 100px;
      height: 100px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      background-color: yellow;
      box-shadow: 0 0 30px 3px gold;
      border-radius: 50%;
    }
    .earth {
      width: 350px;
      height: 350px;
      position: absolute;
      left: 50%;
      top: 50%;
      /* 保证居中 */
      transform: translate(-50%, -50%);
      border: 1px solid #ccc;
      border-radius: 50%;

      animation: rot 18s linear infinite;
    }
    .earth::before {
      content: "";
      width: 40px;
      height: 40px;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translate(-50%, -50%);
      background-color: dodgerblue;
      border-radius: 50%;
    }
    .moon {
      width: 80px;
      height: 80px;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;

      animation: rot 5s linear infinite;
    }
    .moon::before {
      content: "";
      width: 10px;
      height: 10px;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      border-radius: 50%;
    }
    .venus {
      width: 500px;
      height: 500px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #ccc;
      border-radius: 50%;
      
      animation: rot 10s linear infinite;
    }
    .venus::before {
      content: "";
      width: 30px;
      height: 30px;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translate(-50%, -50%);
      background-color: red;
      border-radius: 50%;
    }
    .mars {
      width: 600px;
      height: 600px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #ccc;
      border-radius: 50%;

      animation: rot 12s linear infinite;
    }
    .mars::before {
      content: "";
      width: 50px;
      height: 50px;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translate(-50%, -50%);
      background-color: pink;
      border-radius: 50%;
    }
    @keyframes rot {
      0% {
        transform: translate(-50%, -50%) rotate(0deg);
      }
      100% {
        transform: translate(-50%, -50%) rotate(360deg);
      }
    }
  </style>
</head>
<body>
  <div class="sun"></div>
  <div class="earth">
    <div class="moon"></div>
  </div>
  <div class="venus"></div>
  <div class="mars"></div>
</body>
</html>
  • demo 效果: