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

来源:3-1 JS缓冲动画

蓝蜗牛7788

2016-08-17 15:26

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)

}


写回答 关注

1回答

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

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

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113923 学习 · 1443 问题

查看课程

相似问题