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

สร้างเอฟเฟกต์ Fade Out Up ด้วย CSS Animation


เอฟเฟกต์ Fade Out Up

องค์ประก्ञानจางหายพร้อมเลื่อนขึ้นเล็กน้อย ใช้สำหรับการ dismiss แบบนุ่มนวล ไม่มีการเด้ง (bounce)

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

<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 fadeOutUp {
      0%   { opacity: 1; -webkit-transform: translateY(0); }
      100% { opacity: 0; -webkit-transform: translateY(-20px); }
    }
    @keyframes fadeOutUp {
      0%   { opacity: 1; transform: translateY(0); }
      100% { opacity: 0; transform: translateY(-20px); }
    }

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

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

ขั้นตอนTransform YOpacityพฤติกรรม
0%01มองเห็นชัด ตำแหน่งปกติ
100%-20px (ขึ้น)0จางหายพร้อมลอยขึ้น

เวเรียนท์อื่นๆ ของ Fade Out

  • Fade Out Down: translateY(20px)
  • Fade Out Left: translateX(-20px)
  • Fade Out Right: translateX(20px)
  • Fade Out (scale): scale(0)

การใช้งานจริง: Toast Notification

.toast.hide {
  animation: fadeOutUp 0.3s ease-out forwards;
}