-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.js
52 lines (51 loc) · 1.49 KB
/
animate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Created by Administrator on 2015/12/10.
*/
//返回得到当前属性值函数
function getStyle (obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return window.getComputedStyle(obj,null)[attr];
}
}
//动画函数
function animate (obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var flag = true;
for(var attr in json){
var current = 0;
if(attr == "opacity") {
current = Math.round(parseInt(getStyle(obj,attr)*100)) || 0;
//console.log(current);
}
else {
current = parseInt(getStyle(obj,attr)); // 数值
}
var step = (json[attr] - current) / 10;
step = step>0 ? Math.ceil(step) : Math.floor(step);
if(attr == "opacity"){
if("opacity" in obj.style){
obj.style.opacity = (current + step) /100;
}else{
obj.style.filter = "alpha(opacity = "+(current + step)* 10+")";
}
}else if(attr == "zIndex")
{
obj.style.zIndex = json[attr];
}else{
obj.style[attr] = current + step + "px";
}
if(current != json[attr]){
flag = false;
}
}
if(flag){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30);
}