<!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 : [ //顏色庫
"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ī)的 顏色和文字庫里面的
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>
js實(shí)現(xiàn)下雪效果
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 先看效果圖 代碼:下雪效果 下雨效果: 煙花效果 Demo地址:https://github.com/yuying...
- canvas實(shí)現(xiàn)簡單的下雪效果 快到找工作的時(shí)候,我也開始想著寫個(gè)什么小東西好...
- GitHub:使用CAEmitterLayer實(shí)現(xiàn)下雪和煙花效果
- 原生JS實(shí)現(xiàn)Tab切換效果 效果展示 原生JS實(shí)現(xiàn)模態(tài)框效果 效果展示