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>

猜你喜欢

转载自www.cnblogs.com/xinlvtian/p/11615978.html