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

สร้างเอฟเฟกต์ Flip (หมุน 360°) ด้วย CSS Animation


เอฟเฟกต์ Flip

องค์ประกอบหมุนรอบแกน Y 360° พร้อมแอนิเมชัน timing function ที่ซับซ้อน (ease-out → ease-in) และการย่อ-ขยายเล็กน้อย สร้าง感覺ของการพลิกบัตร 3 มิติ

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

<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: 1s;
      animation-duration: 1s;
      -webkit-animation-fill-mode: both;
      animation-fill-mode: both;
    }

    @-webkit-keyframes flip {
      0%   { -webkit-transform: perspective(400px) rotateY(0);   -webkit-animation-timing-function: ease-out; }
      40%  { -webkit-transform: perspective(400px) translateZ(150px) rotateY(170deg); -webkit-animation-timing-function: ease-out; }
      50%  { -webkit-transform: perspective(400px) translateZ(150px) rotateY(190deg) scale(1); -webkit-animation-timing-function: ease-in; }
      80%  { -webkit-transform: perspective(400px) rotateY(360deg) scale(0.95); -webkit-animation-timing-function: ease-in; }
      100% { -webkit-transform: perspective(400px) scale(1); -webkit-animation-timing-function: ease-in; }
    }
    @keyframes flip {
      0%   { transform: perspective(400px) rotateY(0);   animation-timing-function: ease-out; }
      40%  { transform: perspective(400px) translateZ(150px) rotateY(170deg); animation-timing-function: ease-out; }
      50%  { transform: perspective(400px) translateZ(150px) rotateY(190deg) scale(1); animation-timing-function: ease-in; }
      80%  { transform: perspective(400px) rotateY(360deg) scale(0.95); animation-timing-function: ease-in; }
      100% { transform: perspective(400px) scale(1); animation-timing-function: ease-in; }
    }

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

การวิเคราะห์ Keyframes แบบละเอียด

ลอยหน้า จับมุม 170°
%TransformTimingคำอธิบาย
0%rotateY(0)ease-outเริ่มต้น หน้าบน
40%translateZ(150px) rotateY(170°)ease-out
50%translateZ(150px) rotateY(190°) scale(1)ease-inพลิกผ่าน 180° ด้านหลังขึ้น
80%rotateY(360°) scale(0.95)ease-inครบรอบ ย่อเล็กน้อย
100%scale(1)ease-inกลับสู่ขนาดปกติ

ความแตกต่างจาก flipIn/flipOut

  • flip: หมุนครบ 360° กลับสู่สภาพเดิม
  • flipInX/Y: เข้ามาจาก 90° → 0°
  • flipOutX/Y: ออกจาก 0° → 90°