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

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


เอฟเฟกต์ Roll Out

องค์ประกอบหมุนไปพร้อมเลื่อนออกไปขวา จางหาย คล้ายลูกกลิ้งลุยไป ใช้ translateX + rotate ร่วมกัน

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

<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 rollOut {
      0%   { opacity: 1; -webkit-transform: translateX(0px) rotate(0deg); }
      100% { opacity: 0; -webkit-transform: translateX(100%) rotate(120deg); }
    }
    @keyframes rollOut {
      0%   { opacity: 1; transform: translateX(0px) rotate(0deg); }
      100% { opacity: 0; transform: translateX(100%) rotate(120deg); }
    }

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

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

ขั้นตอนTransformOpacityพฤติกรรม
0%translateX(0) rotate(0°)1เริ่มต้นปกติ
100%translateX(100%) rotate(120°)0เลื่อนขวา + หมุน 120° + จางหาย

คู่คู่กับ Roll In

Roll In: translateX(-100%) rotate(-120deg) → translateX(0) rotate(0)

การปรับแต่ง

.rollOut-fast {
  animation: rollOut 0.5s ease-in forwards;
}

.rollOut-spring {
  animation: rollOut 0.8s cubic-bezier(0.68, -0.55, 0.27, 1.55) forwards;
}