为什么去掉var timer=null 动画就一直抖动
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body,div,span{
margin: 0;
padding: 0;
}
#div1{
width: 200px;
height: 200px;
background: red;
position: relative;
left: -200px;
top: 0;
}
#div1 span {
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top: 75px;
}
</style>
<script>
window.onload=function(){
var ddd=document.getElementById('div1');
ddd.onmouseover=function(){
smove(0);
}
ddd.onmouseout=function(){
smove(-200);
}
}
var timer=null;
var speed,taget;
function smove(taget){
clearInterval(timer);
var ddd=document.getElementById('div1');
timer=setInterval(function(){
if(ddd.offsetLeft>taget)
var speed=-10;
else
var speed=10;
if(ddd.offsetLeft==taget)
clearInterval(timer)
else{ddd.style.left=ddd.offsetLeft+speed+'px'}},30)
}
为什么去掉var timer=null 动画就一直抖动 这个timer 在这里起到什么作用