css 动画

74 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Animations</title>
  <style>
    /* 1. 水平移动动画 */
    @keyframes myAnimation {
      0% { transform: translateX(0); }
      50% { transform: translateX(100px); }
      100% { transform: translateX(0); }
    }

    /* 2. 旋转动画 */
    @keyframes rotate {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }

    /* 3. 背景颜色渐变动画 */
    @keyframes changeColor {
      0% { background-color: red; }
      50% { background-color: yellow; }
      100% { background-color: green; }
    }

    /* 4. 水平移动和透明度变化的组合动画 */
    @keyframes move {
      0% { transform: translateX(0); }
      100% { transform: translateX(200px); }
    }

    @keyframes fade {
      0% { opacity: 1; }
      100% { opacity: 0; }
    }

    /* 5. 过渡效果 */
    .box:hover {
      background-color: blue;
      transform: scale(1.2);
    }

    /* 通用样式 */
    .box {
      width: 100px;
      height: 100px;
      margin: 20px;
      display: inline-block;
    }

    /* 应用水平移动动画 */
    .move-box {
      background-color: red;
      animation: myAnimation 2s infinite;
    }

    /* 应用旋转动画 */
    .rotate-box {
      background-color: blue;
      animation: rotate 5s linear infinite;
    }

    /* 应用颜色渐变动画 */
    .color-box {
      animation: changeColor 3s infinite;
    }

    /* 应用移动和透明度组合动画 */
    .combo-box {
      background-color: purple;
      animation: move 4s ease-in-out infinite, fade 2s ease-in-out infinite;
    }

    /* 应用过渡效果的样式 */
    .transition-box {
      background-color: green;
      transition: background-color 1s, transform 0.5s;
    }

  </style>
</head>
<body>
  <div class="box move-box"></div>
  <div class="box rotate-box"></div>
  <div class="box color-box"></div>
  <div class="box combo-box"></div>
  <div class="box transition-box"></div>
</body>
</html>