[html]代码库
无标题文档//实例:随机显示的小星星
/*
(1)网页背景是黑的
(2)星星随机大小:min=15,max=80
(3)星星的坐标是随机的:x_left=0,x_right=(浏览器宽-星星宽)
y_top=0,y_bottom=?
(4)单击某个星星,星星消失
(5)网页加载完成,开始显示星星
(6)定时器:每隔一个周期,插入一个星星
*/
//定义全局变量
var img_width_min = 15;
var img_width_max = 80;
var x_left = 0;
var x_right = window.innerWidth - img_width_max;
var y_top = 0
var y_bottom = window.innerHeight - img_width_max;
;
//定义初始化的函数
function init()
{
document.body.bgColor = "#000";
setInterval("showStar()",1000);//定时器,循环调用showStar()
}
//显示星星
function showStar()
{
var node_img = document.createElement("img");
var width = getRandom(img_width_min,img_width_max);//
var x = getRandom(x_left,x_right);
var y = getRandom(y_top,y_bottom);
node_img.setAttribute("src","images/xingxing.gif");
var style = "position:absolute;left:"+x+"px;top:"+y+"px;";
style += "width:"+width+"px;";
node_img.setAttribute("style",style);
node_img.setAttribute("onclick","removeImg(this)");
document.body.appendChild(node_img);
}
function removeImg(obj)
{
document.body.removeChild(obj);
}
function getRandom(min,max)
{
return Math.floor(Math.random()*(max-min)+min);
}
[源代码打包下载]
随机产生星星.rar(50积分)[7 次下载]