JS實(shí)現(xiàn)炫酷雪花飄落效果
用js實(shí)現(xiàn)漂亮的雪花飄過效果:
步驟:
頁面基本樣式,雪花旋轉(zhuǎn)動(dòng)畫效果
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)動(dòng)畫 snowDiv.style.position = 'absolute' snowDiv.style.top = '0' snowDiv.style.left = '0' snowDiv.style.color = '#fff' document.body.append(snowDiv) // 插入到頁面
接下來,讓元素飄落
animated(snowDiv) // 傳入創(chuàng)建的元素 // 動(dò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)活動(dòng)
let visualWidth = document.body.offsetWidth || document.documentElement.offsetWidth // 頁面可視化寬度 let visualHeight = document.body.offsetHeight || document.documentElement.offsetHeight // 頁面可視化高度 let initPosition = Math.random()*(visualWidth - 80) // 溢出會(huì)有滾動(dòng)條,控制不會(huì)溢出,頁面可視化寬度 - (元素最大寬度 + 最大寬度/2) snowDiv.style.left = initPosition + 'px' // 隨機(jī)在可視化區(qū)域位置內(nèi)生成元素 animated(snowDiv,visualHeight) // 傳入創(chuàng)建的元素 // 動(dò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) }
基本完成:生成一個(gè)隨機(jī)大小/不透明度的元素,并且在可視化區(qū)域內(nèi)飄落
下一步,復(fù)制生成多個(gè)元素:cloneNode()
let minSize = 10 // 生成的最小元素 let maxSize = 50 // 生成的最大元素 let delay = 100 // 生成元素的間隔時(shí)間 let snowDiv = document.createElement('div') // 創(chuàng)建div snowDiv.innerHTML = '❉' // 添加❉內(nèi)容 snowDiv.className = 'snowAnimation' // 添加旋轉(zhuǎn)動(dòng)畫 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) // 溢出會(huì)有滾動(dòng)條,控制不會(huì)溢出,頁面可視化寬度 - (元素最大寬度 + 最大寬度/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) // 動(dòng)態(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) }
到這里就基本完成此效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)現(xiàn)高性能javascript的注意事項(xiàng)
JavaScript代碼在web應(yīng)用程序中經(jīng)常用到,但是很多開發(fā)者忽視了一些性能方面的知識(shí),如何編寫高性能javascript代碼呢?接下來,小編跟大家一起學(xué)習(xí)2019-05-05基于 webpack2 實(shí)現(xiàn)的多入口項(xiàng)目腳手架詳解
這篇文章主要給大家介紹了基于 webpack2 實(shí)現(xiàn)的多入口項(xiàng)目腳手架的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06JavaScript編寫一個(gè)簡(jiǎn)易購物車功能
這篇文章主要為大家詳細(xì)介紹了JavaScript簡(jiǎn)易購物車功能的編寫代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09ionic實(shí)現(xiàn)帶字的toggle滑動(dòng)組件
這篇文章主要為大家詳細(xì)介紹了ionic實(shí)現(xiàn)帶字的toggle滑動(dòng)組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08