<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
div{width:600px;
height:600px;
border:2px solid black;
background-color: gray;
position:relative;}
span{
position:absolute;
color:yellow;
font-size:20px;
}
</style>
</head>
<body>
<input type="button" id='btn1' value="点击" name="">
<input type="button" id='btn2' value="停止" name="">
<div id="dv"></div>
<script type="text/javascript">
var btno1=document.getElementById("btn1");
var btno2=document.getElementById("btn2");
var dvo=document.getElementById("dv");
btno1.οnclick=function(){ timer=setInterval(function(){
dvo.innerHTML="<span>★</span>";
var spano=dvo.firstElementChild;
var x=parseInt(Math.random()*600+1);
var y=parseInt(Math.random()*600+1);
spano.style.left=x+'px';
spano.style.top=y+'px';
},500)
}
btno2.οnclick=function(){clearInterval(timer)}
</script>
</body>
</html>
定时器---星星闪动
最新推荐文章于 2021-03-04 20:55:24 发布