SOURCE

console 命令行工具 X clear

                    
>
console
window.onload=function() {
	var oBtn=document.getElementById('btn1');
	oBtn.onclick=function() {
		startMove(400);
	}
}
    var timer=null;
	function startMove(iTarget) {
	var oDiv=document.getElementById('div1');
	clearInterval(timer);
	var speed;
	if(iTarget>oDiv.offsetLeft)
	{
	 	speed=7;
	}
	else
	{
	 	speed=-7;
	}
     timer=setInterval( function() {  
	 	if(Math.abs(oDiv.offsetLeft-iTarget)<=7)
	 	{     
	 		//oDiv.offsetLeft==iTarget;
	 		//你上面已经写了,明明是不等的,而且还相差速度绝对值的距离,因此改写成
	 		oDiv.style.left=iTarget+'px';//这个值得注意下
	 		clearInterval(timer);
        }
	 	else
	 	{   
            oDiv.style.left=oDiv.offsetLeft+speed+'px';
            document.title=oDiv.offsetLeft+","+speed;
        }
    },30);
	}
<input id="btn1" type="button" value="开始运动">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
#div1 {width: 100px; height: 100px; background: red; position: absolute; top: 50px; left: 900px;}
#div2 { width: 1px; height: 300px; background: black; position: absolute; top: 50px; left: 400px; }
#div3 { width: 1px; height: 300px; background: black; position: absolute; top: 50px; left: 800px; }
#btn1 {margin: 0;}