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

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


เอฟเฟกต์ Bounce In Down

องค์ประกอบสไลด์ลงมาจากด้านบน เด้งเกินจุด แล้วกลับมาตรงตำแหน่ง ใช้สำหรับ header, notification bar, dropdown menu

โค้ดตัวอย่างสมบูรณ์ (แก้ไข typo ในต้นฉบับ)

<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 bounceInDown {
      0%   { opacity: 0; -webkit-transform: translateY(-2000px); }
      60%  { opacity: 1; -webkit-transform: translateY(30px); }
      80%  { -webkit-transform: translateY(-10px); }
      100% { -webkit-transform: translateY(0); }
    }
    @keyframes bounceInDown {
      0%   { opacity: 0; transform: translateY(-2000px); }
      60%  { opacity: 1; transform: translateY(30px); }
      80%  { transform: translateY(-10px); }
      100% { transform: translateY(0); }
    }

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

หมายเหตุ: ต้นฉบับมี typo

ในโค้ดต้นฉบับมี - webkit-animation-name (มีช่องว่าง) ซึ่งไม่ถูกต้อง 已修正为 -webkit-animation-name

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

ขั้นตอนTransform YOpacityพฤติกรรม
0%-2000px (บนนอกจอ)0เริ่มจากบน
60%+30px (ล่างเกิน)1สไลด์ลงมาเกินจุด
80%-10px (ขึ้นเล็กน้อย)1เด้งกลับ
100%01หยุดตรงจุด