打地鼠
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<style>
#d1{ border:1px solid #000; width:600px; height:600px; margin:0 auto; position:relative;}
<!--margin:0 auto;让子元素在父元素内左右居中.“position: relative”是设置定位类型为相对定位,它可以配合“top”、“bottom”、“left”、“right”这4个偏移量来实现相对它所属父元素的相对定位。-->
#d2{width:150px;height:25px;font-size:20px;text-align:center; background:#A69E9E;margin:0 auto; }
#d3{width:200px;height:50px;font-size:25px;text-align:center;cursor:pointer;margin:0 auto; }
<!--cursor:pointer;鼠标移动到此时变成小手-->
</style>
<body>
<div id="d1"></div><br>
<center>
<input id="d2" type="text" readonly value="0"><br><br>
<input id="d3" type="button" value="开始">
</center>>
<script>
d1=document.getElementById("d1");
d2=document.getElementById("d2");
d3=document.getElementById("d3");
d3.onclick=function(){
on_();
}
function rnd_(x1,x2){
return x1+Math.round(Math.random()*(x2-x1));
}
function color_(){
return"#"+Math.floor(Math.random()*16777215).toString(16);
}
function on_(){
if(typeof(t)=="undefined"){
t=setInterval(run_,200);
}
}
function off_(){
clearInterval(t);delete t;
alert("您的得分为"+d2.value+"。");
window.location.reload();
}
function run_(){
o=document.createElement("img");
o.src="mouse.png";
o.style="width:50px;height:50px;position:absolute;left:"+rnd_(10,540)+"px;top:"+rnd_(10,540)+"px;background:"+color_()+";";
o.onmouseover=function(){
this.remove();
d2.value++;
}
d1.appendChild(o);
a=d1.getElementsByTagName("img");
if(a.length>9){
off_();
}
}
</script>
</body>
</html>