JavaScript 實(shí)現(xiàn)下雪特效的示例代碼
更新時間: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 : [ //顏色庫 "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 (){ // 當(dāng)數(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)動 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 實(shí)現(xiàn)下雪特效的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 實(shí)現(xiàn)下雪特效的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Three.js利用Detector.js插件如何實(shí)現(xiàn)兼容性檢測詳解
這篇文章主要給大家介紹了關(guān)于Three.js利用Detector.js插件如何實(shí)現(xiàn)兼容性檢測的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09javascript實(shí)現(xiàn)移動的模態(tài)框效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)移動的模態(tài)框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09“不能執(zhí)行已釋放的Script代碼”錯誤的原因及解決辦法
“不能執(zhí)行已釋放的Script代碼”錯誤的原因及解決辦法...2007-09-09javascript拖拽上傳類庫DropzoneJS使用方法
這篇文章主要介紹了javascript拖拽上傳類庫DropzoneJS使用方法,大家參考使用吧2013-12-12div+css布局的圖片連續(xù)滾動js實(shí)現(xiàn)代碼
整理一個div+css圖片連續(xù)滾動代碼,原理跟腳本之家之前發(fā)布的文章一樣。2010-05-05