问答详情
源自:3-1 JS缓冲动画

为什么除3的时候就不行了

var oDiv=document.getElementById("div1");

oDiv.onmouseover=function(){

startMove(0);

}

oDiv.onmouseout=function(){

startMove(-200);

}

var timer=null;

function startMove(target){

var speed=0;

if(oDiv.offsetLeft<target){

speed=Math.ceil((target-oDiv.offsetLeft)/3);

}else{

speed=Math.floor((target-oDiv.offsetLeft)/5);

}

clearInterval(timer);

timer=setInterval(function(){

if(oDiv.offsetLeft==target){

clearInterval(timer);

}else{

oDiv.style.left=oDiv.offsetLeft+speed+"px";

}

},100)

}


提问者:蓝蜗牛7788 2016-08-17 15:26

个回答

  • Lee晓川
    2016-08-17 20:15:45

    if(oDiv.offsetLeft==target)  可能会超过target