JS缓冲运动
timer=setInterval
(
function()
{
//距离越大速度越快
speed=(target-Div.offsetLeft)/20;
//重要:速度取整,防止出现小数,达不到target
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(Div.offsetLeft==target)
{
clearInterval(timer);
}
else
{
Div.style.left=Div.offsetLeft+speed+"px";
}
},30
)
参数:
- Math.floor(3.98) //3,向下取整
- Math.ceil() //向上取整