// obj是动画对象和target移动到的距离
function anmate(obj, target) {
var timer = setInterval(function() {
if (obj.offsetLeft == target) {
clearInterval(timer);
}
obj.style.left = obj.offsetLeft + 1 + 'px';
}, 30)
}
anmate(div, 300);
<script>
// obj是动画对象和target移动到的距离
function anmate(obj, target) {
var timer = setInterval(function() {
if (obj.offsetLeft == target) {
clearInterval(timer);
}
obj.style.left = obj.offsetLeft + 1 + 'px';
}, 30)
}
anmate(obj, num);// 调用
</script>