JS實(shí)現(xiàn)炫酷雪花飄落效果
用js實(shí)現(xiàn)漂亮的雪花飄過效果:

步驟:
頁面基本樣式,雪花旋轉(zhuǎn)動畫效果
body{
width: 100vw;
height: 100vh;
background-color: #000;
overflow: hidden;
user-select: none;
-webkit-user-select: none;
}
.snowAnimation {
animation: snow 5s infinite linear;
-webkit-animation: snow 5s infinite linear;
}
@keyframes snow {
0%{
transform: rotate(0);
}
100%{
transform: rotate(360deg);
}
}
@-webkit-keyframes snow {
0%{
transform: rotate(0);
}
100%{
transform: rotate(360deg);
}
}
創(chuàng)建雪花,添加樣式
let snowDiv = document.createElement('div') // 創(chuàng)建div
snowDiv.innerHTML = '❉' // 添加❉內(nèi)容
snowDiv.className = 'snowAnimation' // 添加旋轉(zhuǎn)動畫
snowDiv.style.position = 'absolute'
snowDiv.style.top = '0'
snowDiv.style.left = '0'
snowDiv.style.color = '#fff'
document.body.append(snowDiv) // 插入到頁面

接下來,讓元素飄落
animated(snowDiv) // 傳入創(chuàng)建的元素
// 動態(tài)增加元素top值,
function animated(div) {
div.timer = setInterval(() => {
div.style.top = 10 + div.offsetTop + 'px'
},50)
}
接下來,給元素添加隨機(jī)生成的初始化效果
let minSize = 10 // 生成的最小元素 let maxSize = 50 // 生成的最大元素 let randomOpacity = 0.5 + Math.random()*0.5 // 生成元素的不透明度 snowDiv.style.fontSize = minSize + Math.random()*maxSize + 'px' // 元素隨機(jī)大小 snowDiv.style.opacity = randomOpacity // 元素隨機(jī)的不透明度
下一步,添加生成元素的隨機(jī)位置,并且保持可視區(qū)域內(nèi)活動
let visualWidth = document.body.offsetWidth || document.documentElement.offsetWidth // 頁面可視化寬度
let visualHeight = document.body.offsetHeight || document.documentElement.offsetHeight // 頁面可視化高度
let initPosition = Math.random()*(visualWidth - 80) // 溢出會有滾動條,控制不會溢出,頁面可視化寬度 - (元素最大寬度 + 最大寬度/2)
snowDiv.style.left = initPosition + 'px' // 隨機(jī)在可視化區(qū)域位置內(nèi)生成元素
animated(snowDiv,visualHeight) // 傳入創(chuàng)建的元素
// 動態(tài)增加元素top值,當(dāng)元素超過可視化區(qū)域,remove元素
function animated(div,visualHeight) {
div.timer = setInterval(() => {
div.style.top = 10 + div.offsetTop + 'px'
if (Number(div.style.top.replace('px','')) > visualHeight - 80) {
clearInterval(div.timer)
document.body.removeChild(div)
}
},50)
}
基本完成:生成一個隨機(jī)大小/不透明度的元素,并且在可視化區(qū)域內(nèi)飄落
下一步,復(fù)制生成多個元素:cloneNode()
let minSize = 10 // 生成的最小元素
let maxSize = 50 // 生成的最大元素
let delay = 100 // 生成元素的間隔時間
let snowDiv = document.createElement('div') // 創(chuàng)建div
snowDiv.innerHTML = '❉' // 添加❉內(nèi)容
snowDiv.className = 'snowAnimation' // 添加旋轉(zhuǎn)動畫
snowDiv.style.position = 'absolute'
snowDiv.style.top = '0'
snowDiv.style.left = '0'
snowDiv.style.color = '#fff'
let visualWidth = document.body.offsetWidth || document.documentElement.offsetWidth // 頁面可視化寬度
let visualHeight = document.body.offsetHeight || document.documentElement.offsetHeight // 頁面可視化高度
setInterval(() => {
let initPosition = Math.random()*(visualWidth - 80) // 溢出會有滾動條,控制不會溢出,頁面可視化寬度 - (元素最大寬度 + 最大寬度/2)
let randomOpacity = 0.5 + Math.random()*0.5 // 生成元素的不透明度
let speed = 5 + Math.random()*5 // 元素飄落速度
snowDiv.style.fontSize = minSize + Math.random()*maxSize + 'px' // 元素隨機(jī)大小
snowDiv.style.opacity = randomOpacity // 元素隨機(jī)的不透明度
snowDiv.style.left = initPosition + 'px' // 隨機(jī)在可視化區(qū)域位置內(nèi)生成元素
let div = snowDiv.cloneNode(true) // 復(fù)制元素
document.body.append(div) // 添加復(fù)制后的元素
animated(div,speed,visualHeight) // 傳入創(chuàng)建的元素,飄落的速度以及頁面可視化高度
},delay)
// 動態(tài)增加元素top值,當(dāng)元素超過可視化區(qū)域,remove元素
function animated(div,speed,visualHeight) {
div.timer = setInterval(() => {
div.style.top = speed + div.offsetTop + 'px'
if (Number(div.style.top.replace('px','')) > visualHeight - 80) {
clearInterval(div.timer)
document.body.removeChild(div)
}
},50)
}
到這里就基本完成此效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js前端實(shí)現(xiàn)圖片懶加載(lazyload)的兩種方式
本篇文章主要介紹了js前端實(shí)現(xiàn)圖片懶加載(lazyload)的兩種方式 ,使用圖片懶加載可以提高網(wǎng)頁運(yùn)行速度,有興趣的可以了解一下。2017-04-04
JavaScript實(shí)現(xiàn)跟隨滾動緩沖運(yùn)動廣告框
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)跟隨滾動緩沖運(yùn)動廣告框,頁面左右兩邊跟隨式廣告框,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
javascript設(shè)計(jì)模式之中介者模式Mediator
這篇文章主要介紹了javascript設(shè)計(jì)模式之中介者模式Mediator,需要的朋友可以參考下2014-12-12
js跨域和ajax 跨域問題的實(shí)現(xiàn)思路
大家都知道js是不能跨域的,但我們有時候就要這么用,怎么辦呢?辦法總是有的.2009-09-09
JavaScript中顏色模型的基礎(chǔ)知識與應(yīng)用詳解
顏色模型,是用來表示顏色的數(shù)學(xué)模型。比如最常見的?RGB模型,使用?紅綠藍(lán)?三色來表示顏色。本文就來和大家講講JavaScript中顏色模型的基礎(chǔ)知識與應(yīng)用吧2023-02-02
基于Fixed定位的框選功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了基于Fixed定位的框選功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

