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

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


เอฟเฟกต์ Bounce In Right

องค์ประกอบสไลด์เข้ามาจากขวา เด้งเกินจุด แล้วกลับมาตรงตำแหน่ง ใช้สำหรับ sidebar, panel, หรือ element ที่เข้ามาจากขวา

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

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

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

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

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

การใช้งานจริง: Sidebar Slide-in

.sidebar {
  position: fixed;
  right: 0;
  top: 0;
  height: 100vh;
  width: 300px;
  animation: bounceInRight 0.5s ease-out forwards;
}