js运动(定时器)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>js定时器</title>
</head>
<body>
   
</body>
<script>

 var p = document.createElement('p');
 document.body.append(p)
 p.style.width="100px";
 p.style.height='100px'
 p.style.background="red";
 p.style.position="absolute";
 p.style.left='0';
 p.style.top='0';
 var speed = 1;
 var timer =setInterval(function () {
 	 speed+=1;
 	 p.style.left=parseInt(p.style.left)+speed+"px";
 	 p.style.top=parseInt(p.style.top)+speed+"px";
 	  if(parseInt(p.style.left)>200 && parseInt(p.style.top)>200){
          clearInterval(timer)//停止定时器
 	  }
 },30)

    
</script>
</html>

猜你喜欢

转载自blog.csdn.net/qq_39019768/article/details/80413310