使用 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' keyframes 作為 animation 屬性的值。此外,我們可以使用 nth-child CSS 屬性為每個 'snow' div 設定動畫延遲和左側位置。
示例 1
在下面的示例中,我們在 HTML 中建立了多個具有 'snow' 類名的 div 元素。在 CSS 中,我們最初為 snow 元素設定了固定的寬度和高度。此外,我們還為每個 snow 元素設定了背景和位置。
我們添加了 snow 動畫來移動 div 元素並建立雪花效果。此外,我們自定義了每個 snow 元素,並更改了每個 snow 元素的尺寸、不透明度和左側位置。
此外,我們還為每個 snow 元素設定了動畫延遲。因此,我們可以看到雪花元素在不同的時間出現在螢幕上。
<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>
示例 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 的外部第三方庫來建立雪花效果。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP