1 var timer = null;
2
3 function startMove(iTarget){
4 var oDiv = document.getElementById('div1');
5 //h缓冲运动的核心是距离与速度成正比,同时一定需要用到函数取整
6 clearInterval(timer);
7 timer = setInterval(function(){
8 //速度与距离成正比,达到缓冲运动的条件
9 var speed = (iTarget-oDiv.offsetLeft)/10;
10 //距离为正,是向上取整,距离向下 是向下取整
11 speed =speed>0?Math.ceil(speed):Math.floor(speed);
12
13 if (oDiv.offsetLeft>=iTarget) {//是否到达终点
14 clearInterval(timer);//到达终点
15 } else{
16 oDiv.style.left = oDiv.offsetLeft+speed+'px';
17 //到达之前
18 };
19 },30);
20
21 }