Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

สร้างเอฟเฟกต์ Rotate In ด้วย CSS Animation


เอฟเฟกต์ Rotate In

องค์ประกอบหมุนเข้ามาจาก -200° (หมุนหลายรอบ) ไปสู่ 0° พร้อมจางเข้ามา จุดหมุนอยู่กึ่งกลาง (center center)

โค้ดตัวอย่างสมบูรณ์

<html>
<head>
  <style>
    .animated {
      background-image: url(/css/images/logo.png);
      background-repeat: no-repeat;
      background-position: left top;
      padding-top: 95px;
      margin-bottom: 60px;
      -webkit-animation-duration: 10s;
      animation-duration: 10s;
      -webkit-animation-fill-mode: both;
      animation-fill-mode: both;
    }

    @-webkit-keyframes rotateIn {
      0%   { -webkit-transform-origin: center center; -webkit-transform: rotate(-200deg); opacity: 0; }
      100% { -webkit-transform-origin: center center; -webkit-transform: rotate(0); opacity: 1; }
    }
    @keyframes rotateIn {
      0%   { transform-origin: center center; transform: rotate(-200deg); opacity: 0; }
      100% { transform-origin: center center; transform: rotate(0); opacity: 1; }
    }

    .rotateIn {
      -webkit-animation-name: rotateIn;
      animation-name: rotateIn;
    }
  </style>
</head>
<body>
  <div id="animated-example" class="animated rotateIn"></div>
  <button onclick="myFunction()">Reload page</button>
  <script>
    function myFunction() { location.reload(); }
  </script>
</body>
</html>

การวิเคราะห์

ขั้นตอนRotateOpacityพฤติกรรม
0%-200°0หมุนวนรอบๆ จางหาย
100%0°1หยุดตรงหน้า มองเห็นชัด

ความแตกต่าง Rotate In แบบต่างๆ

เอฟเฟกต์Transform OriginRotate เริ่มต้น
rotateIncenter center-200deg (หลายรอบ)
rotateInDownLeftleft bottom-90deg
rotateInDownRightright bottom90deg
rotateInUpLeftleft top90deg
rotateInUpRightright top-90deg

การปรับแต่ง: ใช้ cubic-bezier สำหรับ bounce

.rotateIn-bounce {
  animation: rotateIn 0.8s cubic-bezier(0.68, -0.55, 0.27, 1.55);
  /* overshoot effect */
}