执行下面代码后不知道为什么打开游览器只能正常运行一次第二次就开始乱飘了!
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
<title>速度</title>
<link rel="stylesheet" type="text/css" href="定时器.css">
<script src="速度.js"></script>
</head>
<body>
<div class="div1" id="div1">
<span class="div0">速度</span>
</div>
</body>
</html>
//css
.div1{
width:80px;
height: 80px;
position: relative;
background-color: red;
left: -80px;
}
.div0{
position: absolute;
left: 80px;
background-color: blue;
width: 20px;
height: 40px;
top: 20px;
font-size: 10px;
}
//js
window.onload=function(){
var tanchu=document.getElementById('div1');
tanchu.onmouseover=function(){
startMove(10,0);
}
tanchu.onmouseout=function(){
startMove(-10,-70);
}
}
var timer=null;
function startMove(speed,itarget){
clearInterval(timer);
var tanchu=document.getElementById('div1');
timer=setInterval(function(){
if(tanchu.offsetLeft==itarget){
clearInterval(timer);
}
else
{
tanchu.style.left=tanchu.offsetLeft+speed+'px';
}},3);
}