JavaScript 實現(xiàn)下雪特效的示例代碼
直接上代碼
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>下雪效果實現(xiàn)</title> <link rel='stylesheet' type='text/css' href='http://www.cgvv.com.cn/bcjs/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);// 獲得一個num-1的整數(shù) }, init : function (num){// 最多個數(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 (){ // 當數(shù)組中的數(shù)量,比最大數(shù)量要小的時候 開始創(chuàng)建 if( that.arr.length < that.maxlength){ var d = document.createElement('div'); // 內(nèi)容和 顏色是隨機的 顏色和文字庫里面的 d.innerHTML = that.snowflake[that.random(that.snowflake.length)]; d.style.color = that.snowflakeColor[that.random(that.snowflakeColor.length)]; d.style.position = 'absolute'; // 位置是隨機的 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) }, // 運動 move : function (){var that = this;var arr = that.arr;setInterval(function (){ // 循環(huán)數(shù)組中的每一個元素 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 實現(xiàn)下雪特效的示例代碼的詳細內(nèi)容,更多關于JavaScript 實現(xiàn)下雪特效的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. PHP橋接模式Bridge Pattern的優(yōu)點與實現(xiàn)過程2. asp.net core項目授權流程詳解3. html中的form不提交(排除)某些input 原創(chuàng)4. js select支持手動輸入功能實現(xiàn)代碼5. CSS3中Transition屬性詳解以及示例分享6. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼7. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式8. 開發(fā)效率翻倍的Web API使用技巧9. jsp文件下載功能實現(xiàn)代碼10. ASP常用日期格式化函數(shù) FormatDate()
