最近在学习js 写了个简单的效果,菜鸟可以学习学习,基本原理:使用随即数设置top 和left的值, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>造星星</title> <mce:style><!-- body{margin:0;padding:0;} .sky{background:#000;width:1000;height:500px; position:relative;} #s{ background:#FFF; width:1px; height:1px; overflow:hidden; position:absolute;} --></mce:style><style mce_bogus="1">body{margin:0;padding:0;} .sky{background:#000;width:1000;height:500px; position:relative;} #s{ background:#FFF; width:1px; height:1px; overflow:hidden; position:absolute;}</style> </head> <body> <div class="sky" id="sky"></div> <button id="creatstar" οnclick="creatstars()">没事造星星玩</button> <mce:script type="text/javascript"><!-- function $(id){ return document.getElementById(id);}; function creatstars(){ var star = document.createElement("div"); star.id = "s"; var posX = Math.floor(Math.random(star.style.top)*400+100); var posY = Math.floor(Math.random(star.style.left)*1000+1); $("sky").appendChild(star); star.style.top = posX + "px"; star.style.left = posY + "px"; } // --></mce:script> </body> </html>