.snow{animation:snow7slinearinfinite;}.snow:nth-chil"/> .snow{animation:snow7slinearinfinite;}.snow:nth-chil">

首頁  >  文章  >  web前端  >  使用CSS的降雪動畫效果

使用CSS的降雪動畫效果

王林
王林轉載
2023-09-03 23:25:061131瀏覽

使用CSS的降雪動畫效果

我們可以使用 HTML 和 CSS 來建立動畫。當我們在網頁上添加動畫時,它可以改善應用程式的使用者體驗。我們可以使用 CSS keyframes 屬性來建立各種動畫,並使用「animation」CSS 屬性來使用它。

在本教學中,我們將學習使用CSS創造一個雪花飄落的動畫效果。

文法

使用者可以依照以下語法使用CSS建立降雪動畫效果。

<div class = "snow"> </div>
.snow{
   animation: snow 7s linear infinite;
}
.snow:nth-child(2) {
   left: 20%;
   animation-delay: 1s;
}

在上面的語法中,我們建立了具有「snow」類別名稱的 div 元素,並新增了「snow」關鍵影格作為動畫的值。此外,我們可以使用 nth-child CSS 屬性為每個「雪」div 設定動畫延遲和左側位置。

Example 1

的中文翻譯為:

範例 1

在下面的範例中,我們在 HTML 中建立了多個帶有「snow」類別名稱的 div 元素。在CSS中,我們最初為雪元素設定了固定的寬度和高度。此外,我們還為每個雪元素設定了背景和位置。

我們新增了下雪動畫來移動 div 元素並創建降雪效果。此外,我們還自訂了每個雪元素,並更改了每個雪元素的尺寸、不透明度和左側位置。

此外,我們為每個雪花元素設定了動畫延遲。因此,我們可以看到雪花元素在螢幕上以不同的時間下落。

<html>
<head>
   <style>
      * {background-color: darkblue; color: white;}
      /* add snowfall animation */
      .snow {
         position: absolute;
         top: -50px;
         left: -50px;
         width: 15px;
         height: 15px;
         border-radius: 50%;
         background: white;
         animation: snow 7s linear infinite;
      }
      .snow:nth-child(1) {
         left: 10%;
         opacity: 0.3;
         height: 10px;
         width: 10px;
         animation-delay: 0s;
      }
      .snow:nth-child(2) {
         left: 20%;
         opacity: 0.5;
         animation-delay: 1s;
      }
      .snow:nth-child(3) {
         left: 30%;
         height: 5px;
         width: 10px;
         animation-delay: 2s;
      }
      .snow:nth-child(4) {
         left: 40%;
         height: 8px;
         width: 13px;
         animation-delay: 1s;
      }
      .snow:nth-child(5) {
         left: 50%;
         opacity: 0.7;
         animation-delay: 4s;
      }
      .snow:nth-child(6) {
         left: 60%;
         opacity: 0.3;
         height: 20px;
         width: 13px;
         animation-delay: 8s;
      }
      .snow:nth-child(7) {
         left: 70%;
         opacity: 0.9;
         height: 17px;
         width: 10px;
         animation-delay: 6s;
      }
      .snow:nth-child(8) {
         left: 80%;
         opacity: 0.6;
         animation-delay: 7s;
      }
      .snow:nth-child(9) {
         left: 90%;
         height: 12px;
         width: 12px;
         animation-delay: 5s;
      }
      .snow:nth-child(10) {
         left: 80%;
         height: 22px;
         width: 16px;
         animation-delay: 9s;
      }
      @keyframes snow {
         0% {
            transform: translateX(0) translateY(0);
         }
         100% {
            transform: translateX(80px) translateY(1000px);
         }
      }
   </style>
</head>
<body>
   <h2> Adding the <i> Snowfall animation </i> using HTML and CSS. </h2>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
   <div class = "snow"> </div>
</body>
</html>

Example 2

的中文翻譯為:

範例2

在下面的範例中,我們使用了'Jquery-snowfall'函式庫來使用Jquery創建了一個下雪效果。我們使用CDN將該庫添加到了

部分。

在 jQuery 中,我們呼叫 Snowfall() 方法來建立降雪效果。此外,我們也向 Snowfall() 方法傳遞了一些參數。

在輸出中,使用者可以觀察到降雪效果,比上面的範例好。

<html>
<head>
   <style>
      * {
         color: blue;
      }
      .snow-fall {
         height: 600px;
         width: 600px;
         background-color: blue;
      }
   </style>
   <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
   <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQuery-Snowfall/1.7.4/snowfall.jquery.min.js"> </script>
</head>
<body>
   <h2> Adding the <i> Snowfall animation </i> using HTML and JQuery. </h2>
   <div class = "snow-fall"> </div>
   <script>
      $('.snow-fall').snowfall({ flakeCount: 500, maxSpeed: 2, maxSize: 10 });
   </script>
</body>
</html>

使用者學習了兩種不同的方法來創建降雪效果。在第一種方法中,我們只使用了 HTML 和 CSS。開發人員可以觀察到程式碼非常複雜且難以閱讀,因為它需要建立每個雪元素並使用 CSS 對其進行操作。在第二種方法中,我們使用jQuery的外部第三方函式庫來建立降雪效果。

以上是使用CSS的降雪動畫效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除