這次給大家?guī)鞪S實現(xiàn)動態(tài)雪花飄落,JS實現(xiàn)動態(tài)雪花飄落的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。
<!DOCTYPE html> <html> <head> <meta content="text/html" charset="utf-8"> <title>飄雪</title> <style type="text/css"> *{ margin:0; padding:0; font-family:"微軟雅黑"; font-size:13px; color:#ffffff; } body{ background:#00a0fc; overflow:hidden; } </style> </head> <body> </body> <script type="text/javascript" src="js/jquery-1.8.3.js"></script> <script type="text/javascript"> // 窗口大小 var oPageX=window.screen.width; var oPageY=window.screen.height; var aSnow=[]; // 雪花模板 function snow(x,y,xspeed,yspeed,xsin,size){//x水平位置,y垂直位置,xspeed水平步距,yspeed垂直步距,xsin振幅,size雪花大小 this.node=document.createElement('p'); this.x=x; this.y=y; this.xspeed=xspeed; this.yspeed=yspeed; this.xsin=xsin; this.size=size; this.createSnow=function(){ this.node.style.position='absolute'; this.node.style.top=this.y+'px'; this.node.style.left=this.x+'px'; this.node.innerHTML='*'; document.body.appendChild(this.node); }; this.createSnow(); this.snowMove=function(){ this.node.style.top=parseInt(this.node.style.top)+this.yspeed+'px';//垂直方向運動 this.node.style.left=parseInt(this.node.style.left)+this.xsin*Math.sin(this.xspeed)+'px'; this.node.style.fontSize=this.size+'px'; // this.node.style.color='rgb('+Math.ceil(Math.random()*255)+','+Math.ceil(Math.random()*255)+','+Math.ceil(Math.random()*255)+')'; }; } // 創(chuàng)建雪花 function createSnow(){ aSnow.push(new snow(Math.random()*oPageX,-50,0.02+Math.random()/10,1+Math.random(),Math.random()*30,20+Math.random()*30)); } setInterval(createSnow,1000);//一秒鐘創(chuàng)建一個雪花 // 雪花移動函數(shù) function snowMove(){ for(var j=0;j<aSnow.length;j++){ aSnow[j].snowMove(); if(parseInt(aSnow[j].node.style.top)>oPageY || parseInt(aSnow[j].node.style.left)>oPageX){ aSnow[j].node.parentNode.removeChild(aSnow[j].node); aSnow.splice(j,1); } } } setInterval(snowMove,10); </script> </html>
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請關(guān)注Gxl網(wǎng)其它相關(guān)文章!
推薦閱讀:
JS+canvas繪制餅狀統(tǒng)計圖
react-native-fs插件使用案列詳解
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com