 
		慕丝7289332
2016-08-03 12:08
var timer=null;
window.onload=function () {
    var oDiv=document.getElementById('div1');
    oDiv.onmouseover=function () {
        startMove(0);
    }
    oDiv.onmouseout=function () {
        startMove(-200);
    }
}
function startMove(iTarget) {
    clearInterval(timer);
    var oDiv=document.getElementById('div1');
    timer=setInterval(function () {
        var speed=0;
        if (oDiv.style.left>iTarget){
            speed=-10;
        }else {
            speed=10;
        }
        if (oDiv.offsetLeft==iTarget){
            clearInterval(timer);
        }else{
            oDiv.style.left=oDiv.offsetLeft+speed+'px';
        }
    },30)
}
 
				    function moveS(iTarget){
        var div1=document.getElementById('div1');
        clearInterval(timer);
        timer=setInterval(function(){
            var speed;
            if(div1.offsetLeft>iTarget){
                speed=-10;
            }else{
                speed=10;
            }
            if(div1.offsetLeft==iTarget ){
                clearInterval(timer);
            }else{
                div1.style.left=div1.offsetLeft + speed + 'px';
            }
        },30)
    }
 
				当他完全右移出来后 你没有判断
加一句
if(oDiv.offsetLeft==0) {
clearInterval(timer);}
让计时器停止工作就行了
JS动画效果
113910 学习 · 1502 问题
相似问题