<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>缓动框架</title>
</head>
<body>
</body>
</html>
<script>
function animate(obj, attr, target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
// leader = 当前元素的属性值
var leader =parseInt(getStyle(obj,attr));
// 缓动公式step =(target - leader)/10 leader = leader + step;
var step =(target - leader)/10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
leader = leader + step;
// 赋值给元素
obj.style[attr] = leader +"px";
if(leader == target){
clearInterval(obj.timer);
}
},20)
}
// 获取样式的方法 元素 属性
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return window.getComputedStyle(obj, null)[attr];
}
};
</script>