js定时器加运动基础

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {width:200px;height:200px;background-color:red;position:absolute;top:50px;left:0px;}
</style>
</head>
<body>
<script>
var time=null;
function move(){
var odiv=document.getElementById('div1');
clearInterval(time);
time=setInterval (function(){
if(odiv.offsetLeft>=300)
{
clearInterval(time);
}
else{
odiv.style.left=odiv.offsetLeft+7+'px';
}
},20);
}
</script>
<div id="div1"></div>
<input type="button" value="点击" onClick="move()" />
</body>
</html>

 

posted @ 2019-10-01 19:50  绿林豪士  阅读(212)  评论(0编辑  收藏  举报