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

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


เอฟเฟกต์ Flip In X

องค์ประกอบพลิกเข้ามาจากแกน X (แนวนอน) เริ่มจาก 90° (มองไม่เห็น) หมุนมาเป็น 0° พร้อมเด้ง ใช้สำหรับการ์ดพลิก โชว์ข้อมูล

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

<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 flipInX {
      0%   { -webkit-transform: perspective(400px) rotateX(90deg); opacity: 0; }
      40%  { -webkit-transform: perspective(400px) rotateX(-10deg); }
      70%  { -webkit-transform: perspective(400px) rotateX(10deg); }
      100% { -webkit-transform: perspective(400px) rotateX(0deg); opacity: 1; }
    }
    @keyframes flipInX {
      0%   { transform: perspective(400px) rotateX(90deg); opacity: 0; }
      40%  { transform: perspective(400px) rotateX(-10deg); }
      70%  { transform: perspective(400px) rotateX(10deg); }
      100% { transform: perspective(400px) rotateX(0deg); opacity: 1; }
    }

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

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

ขั้นตอนRotate XOpacityพฤติกรรม
0%90°0เริ่มหมุนนอน (มองไม่เห็น)
40%-10°-หมุนเกินจุด (บน)
70%10°-เด้งกลับ (ล่าง)
100%0°1หยุดตรงหน้า

การประยุกต์: Card Flip

.card {
  perspective: 1000px;
}
.card-inner {
  transition: transform 0.6s;
  transform-style: preserve-3d;
}
.card:hover .card-inner {
  transform: rotateY(180deg);
}
.card-front, .card-back {
  backface-visibility: hidden;
  position: absolute;
}
.card-back { transform: rotateY(180deg); }