<!DOCTYPE HTML>
<html>
<head>
<title>js动画animate缓动效果setInterval()</title>
<meta charset="utf-8" />
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 100px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box"> </div>
<script>
var box = document.getElementById('box');
function animate(obj, target) {
function move() {
obj.style.marginLeft = obj.offsetLeft + (target - obj.offsetLeft) / 10 + 'px';
if(obj.offsetLeft == target) {
clearTimeout(obj.timer);
}
}
obj.timer = setInterval(move, 100);
}
animate(box, 300);
</script>
</body>
</html>