我们可以使用 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的降雪动画效果的详细内容。