使用CSS為直線運動中的直線設置動畫


HTML:在HTML中,我們創建了一個div元素,用於制作一條直線。

<!DOCTYPE html> <html lang="en">
<head>     <meta charset="UTF-8" />     <meta name="viewport" content=         "width=device-width, initial-scale=1.0" />     <title>GeeksforGeeks</title> </head>
<body>     <div class="geeks"></div> </body>
</html>

 

CSS:

  • 通過提供您喜歡的最小高度和寬度來創建一條直線。

  • 使用before選擇器對此直線進行動畫處理,並為其提供線性動畫,並帶有名為animate的關鍵幀標識符。

  • 關鍵幀的動畫非常簡單。對於前半幀,使寬度為100%(向前移動),然后將其減小為下半幀的0%(向后移動)。

<style>     body {         margin: 0;         padding: 0;         background: green;     }
    .geeks {         width: 400px;         height: 2px;         background: #fff;         position: absolute;         top: 50%;         left: 50%;         transform: translate(-50%, -50%);     }
    .geeks::before {         content: "";         position: absolute;         top: 0;         left: 0;         width: 100%;         height: 100%;         background: green;         animation: animate 5s linear infinite;     }
    @keyframes animate {         0% {             left: 0;         }
        50% {             left: 100%;         }
        0% {             left: 0;         }     } </style>

 

完整代碼:在本節中,我們將結合使用HTML和CSS代碼來制作直線動畫效果。

<!DOCTYPE html> <html lang="en">
<head>     <meta charset="UTF-8" />     <meta name="viewport" content=         "width=device-width, initial-scale=1.0" />
    <title>         How to animate a straight         line in linear motion?     </title>
    <style>         body {             margin: 0;             padding: 0;             background: green;         }
        .geeks {             width: 400px;             height: 2px;             background: #fff;             position: absolute;             top: 50%;             left: 50%;             transform: translate(-50%, -50%);         }
        .geeks::before {             content: "";             position: absolute;             top: 0;             left: 0;             width: 100%;             height: 100%;             background: green;             animation: animate 5s linear infinite;         }
        @keyframes animate {             0% {                 left: 0;             }
            50% {                 left: 100%;             }
            0% {                 left: 0;             }         } </style> </head>
<body>     <div class="geeks"></div> </body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM