-
Notifications
You must be signed in to change notification settings - Fork 0
/
jslib.js
37 lines (34 loc) · 956 Bytes
/
jslib.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
function animate(elem, attr, callback){
clearInterval(elem.timer);
elem.timer = setInterval(function(){
var bStop = true;//一个标识位,true代表可以停止定时器,false代表不可不停止
for(var prop in attr){//1:width
var curr = parseInt(getStyle(elem, prop));
if(prop == 'opacity'){
curr = parseInt(getStyle(elem, prop)*100);
}
var speed = (attr[prop] - curr) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if(curr != attr[prop]){
bStop = false;
}
if(prop == 'opacity'){
elem.style.opacity = (curr + speed) / 100;
elem.style.filter = 'alpha(opacity='+(curr + speed)+')';
}else{
elem.style[prop] = curr + speed + 'px';
}
}
if(bStop){
clearInterval(elem.timer);
callback && callback();
}
}, 30);
}
function getStyle(elem, attr){
if(elem.currentStyle){//IE
return elem.currentStyle[attr];
}else{
return getComputedStyle(elem, false)[attr];
}
}