这次只需要输入结束点坐标target。 向左向右需要更改left为right
function startMove(target){
//清除之前的计时器
clearInterval(timer);
if(box.offsetLeft > target){
speed = -10;
}else {
speed = 10;
}
//定义新的计时器
timer = setInterval(function(){
if(box.offsetLeft == target){
clearInterval(timer);
}else {
box.style.left = box.offsetLeft+ speed +'px';
}
},30)
}