為什么鼠標(biāo)移開時,會出現(xiàn)不停抖動
window.onload=function(){
var divv=document.getElementById("div1");
? ?divv.onmouseover =function(){
? ? startMove(10,0);
? ?}
? ?divv.onmouseout=function(){
? ? startMove(-10,-200);
? ?}
}
?var timer=null;
function startMove(speed,Target){
clearInterval(timer);
divv=document.getElementById("div1");
setInterval(function(){
if (divv.offsetLeft == Target) {
clearInterval(timer);
}else{
divv.style.left=divv.offsetLeft+speed+'px';
}
},30)
}
2017-02-24
timer=setInterval(function() { //定時器賦值給timer,要不然timer一直都是null吧;
? ? ? ? ? ? if (divv.offsetLeft == Target) {
? ? ? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? divv.style.left = divv.offsetLeft + speed + 'px';
? ? ? ? ? ? }
? ? ? ? }, 30)
2017-02-25
鼠標(biāo)移開時,不停抖動,上一次的計時器還在運(yùn)行吧