function animate(obj,target){var id = setInterval(function(){if(obj.offsetLeft >= target){clearInterval(id);}else{obj.style.left = obj.offsetLeft + 5 + 'px';}},30)
}
可以实现如下效果:
<div></div><span></span>
<style>div{width: 100px;height: 100px;background-color: red;position: absolute;top: 0;left: 0;}span{display: block;width: 50px;height: 50px;background-color: gold;margin-top: 200px;position: absolute;top: 0;left: 0;}</style>
function animate(obj,target){var id = setInterval(function(){if(obj.offsetLeft >= target){obj.offsetLeft = target;clearInterval(id);}else{obj.style.left = obj.offsetLeft + 5 + 'px';}},30)}var div = document.querySelector('div');var span = document.querySelector('span');animate(div,300);animate(span,300);