微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用CSS的降雪动画效果

使用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将该库添加到了<head>部分。

在 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的降雪动画效果的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。