js代碼:飄落的雪花


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>

</head>

<script type="text/javascript">
var snow_size = new Array(); //大小;
var snow_color = new Array(); //顏色
var num = 30; //個數
var smallest = 5; //最小像素的雪花
var largest = 30; //最大像素(35)
var dx = new Array(); //
var x_pos = new Array();
var y_pos = new Array();
var x_amplitude = new Array();
var x_step = new Array();
var y_step = new Array();
var win_width;
var win_height;
var scroll_x;
var scroll_y;
var interval = 100; //毫秒
var orcolor=true;

function make_size(){
return smallest + Math.random()*largest;
}

function make_color(){
if(orcolor){
for(i=0; i<num; i++){
snow_color[i] = '#ffffff';
}
}else{
for(i=0; i<num; i++){
A = Math.ceil(Math.random()*255);
B = Math.ceil(Math.random()*255);
C = Math.ceil(Math.random()*255);
snow_color[i] = 'rgb(' + A + ',' + B + ',' + C + ')';
}
}
}

function make_color2(){

}

function init(){
var id_body = document.getElementById("id_body");
win_width = window.innerWidth ? window.innerWidth : id_body.clientWidth;
win_height = window.innerHeight ? window.innerHeight : id_body.clientHeight;
scroll_x = id_body.scrollLeft;
scroll_y = id_body.scrollTop;
make_color2();

for(i=0; i<num; i++){
dx[i] = 0;
x_pos[i] = Math.random()*(win_width + scroll_x - 40);
y_pos[i] = Math.random()*(win_height + scroll_y);
x_amplitude[i] = Math.random()*20;
snow_size[i] = make_size();
x_step[i] = 0.02 + Math.random()/10;
y_step[i] = 0.7 + Math.random();
}
}

function add_div(){
var id_body = document.getElementById("id_body");
win_width = window.innerWidth ? window.innerWidth : id_body.clientWidth;
win_height = window.innerHeight ? window.innerHeight : id_body.clientHeight;

scroll_x = id_body.scrollLeft;
scroll_y = id_body.scrollTop;
make_color();


for(i=0; i<num; i++){
dx[i] = 0;
x_pos[i] = Math.random()*(win_width + scroll_x - 40);
y_pos[i] = Math.random()*(win_height + scroll_y);
x_amplitude[i] = Math.random()*20;
snow_size[i] = make_size();
x_step[i] = 0.02 + Math.random()/10;
y_step[i] = 0.7 + Math.random();
document.write("<div id='snow_"+i+"' style='position: absolute;z-index: eval(30"+ i +"); visibility: visible; top: 15px; left:15px; font-size:"+snow_size[i]+"px"+";color:"+snow_color[i]+"'>*</div>");
}
}

function snow(){
var id_body = document.getElementById("id_body");
win_width = window.innerWidth ? window.innerWidth : id_body.clientWidth;
win_height = window.innerHeight ? window.innerHeight : id_body.clientHeight;
scroll_x = id_body.scrollLeft;
scroll_y = id_body.scrollTop;


for(i=0; i<num; i++){
y_pos[i] += y_step[i];
if (y_pos[i] > win_height + scroll_y - 50){
x_pos[i] = Math.random()*(win_width + scroll_x - x_amplitude[i] -20);
y_pos[i] = 0;
x_step[i] = 0.02 + Math.random()/10;
y_step[i] = 0.7 + Math.random();
}
dx[i] += x_step[i];

document.getElementById("snow_"+i).style.top = y_pos[i];
document.getElementById("snow_"+i).style.left = x_pos[i] + x_amplitude[i]*Math.sin(dx[i]);
}
setTimeout("snow()",interval);
}
function orclick(){
if(orcolor){
orcolor = false;}
else{orcolor = true;}
alert(orcolor);
}

</script>
<body id="id_body" style="background-color:#666666;">
<table height="700px" width="300px" align="center">
<tr height="200px"><td></td></tr>
<tr height="21px">
</tr>
<tr height="21px">
</tr>
</table>
<script type="text/javascript">
add_div();
snow();firefox沒效果要IE的
</script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章