修改 totalSnow 和 dropHeight来改变雪花的数量和初始显示区域的高度。肯定可以优化,但我水平太滥,而且也不太清楚到底哪个方法节约资源,要是经典论坛的小狗和LexRus来了,肯定意见一大堆,:)
//2004-12-30 by guoshuang for snow in xi'an
totalSnow=40; //snow numbers
dropHeight=80;
oMode=5;
imgsrc="images/snowb.gif"
var oNodes;
function initSnow(){
//random config the property of snow image
document.getElementById("oSnow").innerHTML="";
for(i=0;i snowimg=document.createElement("img"); snowimg.src=imgsrc; snowimg.alt=""; snowimg.className="snowPosition"; snowimg.width=parseInt(Math.random()*28); snowimg.height=snowimg.width; snowimg.style.left=parseInt(Math.random()*document.body.clientWidth-28)+"px"; snowimg.style.top=parseInt(Math.random()*dropHeight)+"px"; document.getElementById("oSnow").appendChild(snowimg); } oNodes=document.getElementById("oSnow").childNodes; } function dropSnow(){ if(oNodes.length==0)return; for(j=0;j nowTop=oNodes[j].style.top; nowLeft=oNodes[j].style.left; nowTop=nowTop.substring(0,nowTop.length-2); nowLeft=nowLeft.substring(0,nowLeft.length-2); nowWidth=oNodes[j].clientWidth; oNodes[j].style.top=parseInt(nowTop)+2; //special show for random left right moving leftDistance=parseInt(Math.random()*3) oNodes[j].style.left=parseInt(nowLeft)-leftDistance; if(nowTop%oMode==0){ if(nowWidth<1){ //remove old node,append new one! document.getElementById("oSnow").removeChild(oNodes[j]); //new one snowimg=document.createElement("img"); snowimg.src=imgsrc; snowimg.alt=""; snowimg.className="snowPosition"; snowimg.width=parseInt(Math.random()*28); snowimg.height=snowimg.width; snowimg.style.left=parseInt(Math.random()*document.body.clientWidth-28)+"px"; snowimg.style.top=parseInt(Math.random()*dropHeight)+"px"; document.getElementById("oSnow").appendChild(snowimg); }else{ oNodes[j].style.width=nowWidth-1; oNodes[j].style.height=nowWidth-1; } } } } //init window.onload=function(){initSnow();setInterval(dropSnow,200)} window.onresize=function(){initSnow()} 简化版,只显示,不动,拖动窗口大小,图案变化 //2004-12-30 by guoshuang for snow in xi'an totalSnow=40; //snow numbers dropHeight=80; imgsrc="images/snowb.gif" var oNodes; function initSnow(){ //random config the property of snow image document.getElementById("oSnow").innerHTML=""; for(i=0;i snowimg=document.createElement("img"); snowimg.src=imgsrc; snowimg.alt=""; snowimg.className="snowPosition"; snowimg.width=parseInt(Math.random()*28); snowimg.height=snowimg.width; snowimg.style.left=parseInt(Math.random()*document.body.clientWidth-28)+"px"; snowimg.style.top=parseInt(Math.random()*dropHeight)+"px"; document.getElementById("oSnow").appendChild(snowimg); } oNodes=document.getElementById("oSnow").childNodes; } //init window.onresize=function(){initSnow()}
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
Leave a reply