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

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


เอฟเฟกต์ Roll In

องค์ประกอบบินเข้ามาจากซ้าย พร้อมหมุนจาก -120° สู่ 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: 10s;
      animation-duration: 10s;
      -webkit-animation-fill-mode: both;
      animation-fill-mode: both;
    }

    @-webkit-keyframes rollIn {
      0%   { opacity: 0; -webkit-transform: translateX(-100%) rotate(-120deg); }
      100% { opacity: 1; -webkit-transform: translateX(0px) rotate(0deg); }
    }
    @keyframes rollIn {
      0%   { opacity: 0; transform: translateX(-100%) rotate(-120deg); }
      100% { opacity: 1; transform: translateX(0px) rotate(0deg); }
    }

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

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

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

คู่คู่ Roll In / Roll Out

.roll-in  { animation: rollIn  0.6s ease-out forwards; }
.roll-out { animation: rollOut 0.4s ease-in  forwards; }

/* Staggered list */
.item:nth-child(1) { animation-delay: 0.0s; }
.item:nth-child(2) { animation-delay: 0.1s; }
.item:nth-child(3) { animation-delay: 0.2s; }

การปรับแต่ง Timing Function

/ ความรู้สึกต่างๆ /
ease-out          /* เริ่มเร็ว ช้าลง - ธรรมชาติ */
cubic-bezier(0.68, -0.55, 0.27, 1.55)  /* overshoot bounce */
cubic-bezier(0.17, 0.67, 0.83, 0.67)   /* smooth elastic */