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.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;}