為什么去掉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 在這里起到什么作用
2017-06-08
time不定義的話是沒有這個變量的
2017-04-24
timer要先定義?