js缓冲运动

js缓冲运动_第1张图片

var btn = document.getElementById('btn');

var div1 = document.getElementById('div1');

var tiemr;

btn.onclick = function(){

tiemr = setInterval(function(){

var speed = (300 - div1.offsetLeft)/10;

// speed = Math.ceil(speed); //向上取整

speed = speed>0?Math.ceil(speed):Math.floor(speed);//大于零向上,小于零向下

if (div1.offsetLeft == 300) {

clearInterval(tiemr);

} else {

div1.style.left = div1.offsetLeft + speed + 'px';

}

},30)

}

你可能感兴趣的:(js缓冲运动)