JavaScript 實(shí)現(xiàn)下雪特效的示例代碼
更新時(shí)間:2020年09月09日 11:02:41 作者:崔笑顏
這篇文章主要介紹了JavaScript 實(shí)現(xiàn)下雪特效的示例代碼,幫助大家利用JavaScript制作特效,感興趣的朋友可以了解下
直接上代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下雪效果實(shí)現(xiàn)</title>
<link rel="stylesheet" type="text/css" href="reset.css">
<style type="text/css">
body,html{
overflow: hidden;
}
</style>
</head>
<body>
<script type="text/javascript">
var snowflakes = {
arr:[],// 數(shù)組盛放元素
snowflake : [//雪花類型
'❉',
'❈',
'*',
'✲',
'❀',
'❃'
],
snowflakeColor : [ //顏色庫(kù)
"red",
"green",
"#ccc123",
"#345232",
"#231111",
"#ab2322"
],
random : function (num){
return Math.floor(Math.random()*num);// 獲得一個(gè)num-1的整數(shù)
},
init : function (num){
// 最多個(gè)數(shù)
this.maxlength = num;
// 邊界
this.maxWidth = (document.documentElement.clientWidth || document.body.clientWidth) + 20;
// 邊界
this.maxHeight = (document.documentElement.clientHeight || document.body.clientHeight) + 20;
this.create();
this.move();
},
// 創(chuàng)建
create : function (){
var that = this;
setInterval(function (){
// 當(dāng)數(shù)組中的數(shù)量,比最大數(shù)量要小的時(shí)候 開始創(chuàng)建
if( that.arr.length < that.maxlength){
var d = document.createElement("div");
// 內(nèi)容和 顏色是隨機(jī)的 顏色和文字庫(kù)里面的
d.innerHTML = that.snowflake[that.random(that.snowflake.length)];
d.style.color = that.snowflakeColor[that.random(that.snowflakeColor.length)];
d.style.position = "absolute";
// 位置是隨機(jī)的 top(0- -99) left (0 - that.maxWidth*2/3-1)
d.style.left = that.random(that.maxWidth*2/3) + "px";
d.style.top = -that.random(100) + "px";
// 速度
d.vx = 2+that.random(10);
d.vy = 3+that.random(10);
// 數(shù)組添加元素, body 添加元素
document.body.appendChild(d);
that.arr.push(d)
}
},20)
},
// 運(yùn)動(dòng)
move : function (){
var that = this;
var arr = that.arr;
setInterval(function (){
// 循環(huán)數(shù)組中的每一個(gè)元素
for(var i = 0 ; i < arr.length ; i ++ ){
// 替換位置
arr[i].style.left = arr[i].offsetLeft + arr[i].vx + "px";
arr[i].style.top = arr[i].offsetTop + arr[i].vy + 'px';
// 判斷邊界 刪除元素
if (arr[i].offsetTop >= that.maxHeight || arr[i].offsetLeft >= that.maxWidth) {
document.body.removeChild(arr[i]);
arr.splice(i,1);
}
}
},30)
}
}
window.onload = function (){
snowflakes.init(100);
}
</script>
</body>
</html>
效果圖

以上就是JavaScript 實(shí)現(xiàn)下雪特效的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 實(shí)現(xiàn)下雪特效的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js計(jì)算字符串長(zhǎng)度包含的中文是utf8格式
使用js寫的計(jì)算字符串長(zhǎng)度且其中中文是utf8格式的,具體的實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-10-10
Javascript中replace方法與正則表達(dá)式的結(jié)合使用教程
replace方法是javascript涉及到正則表達(dá)式中較為復(fù)雜的一個(gè)方法,嚴(yán)格上說應(yīng)該是string對(duì)象的方法,下面這篇文章主要給大家介紹了關(guān)于Javascript中replace方法與正則表達(dá)式的結(jié)合使用的相關(guān)資料,需要的朋友可以參考下2022-09-09
js獲取form表單所有數(shù)據(jù)的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇js獲取form表單所有數(shù)據(jù)的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-08-08
微信小程序?qū)W習(xí)筆記之登錄API與獲取用戶信息操作圖文詳解
這篇文章主要介紹了微信小程序?qū)W習(xí)筆記之登錄API與獲取用戶信息操作,結(jié)合實(shí)例形式分析了微信小程序登陸請(qǐng)求及后臺(tái)交互相關(guān)操作技巧,并結(jié)合圖文形式進(jìn)行說明,需要的朋友可以參考下2019-03-03

