-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathstartmove.js
52 lines (46 loc) · 1.19 KB
/
startmove.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
function startMove(obj, json, fnEnd) {
if (obj.timer) {
clearInterval(obj.timer);
}
obj.timer = setInterval(function() {
doMove(obj, json, fnEnd);
}, 10);
var oDate = new Date();
if (oDate.getTime() - obj.lastMove > 30) {
doMove(obj, json, fnEnd);
}
}
function getStyle(obj, attr){
return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj, false)[attr];
}
function doMove(obj, json, speed, fnEnd) {
var iCur = 0;
var attr = '';
var _speed = speed || 8;
var bStop = true; //假设运动已经该停止了
for (attr in json) {
iCur = attr == 'opacity' ? parseInt(100 * parseFloat(getStyle(obj, 'opacity'))) : parseInt(getStyle(obj, attr));
if (isNaN(iCur)) {
iCur = 0;
}
var iSpeed = (json[attr] - iCur) / _speed;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if (parseInt(json[attr]) != iCur) {
bStop = false;
}
if (attr == 'opacity') {
obj.style.filter = "alpha(opacity:" + (iCur + iSpeed) + ")";
obj.style.opacity = (iCur + iSpeed) / 100;
} else {
obj.style[attr] = iCur + iSpeed + 'px';
}
}
if (bStop) {
clearInterval(obj.timer);
obj.timer = null;
if (fnEnd) {
fnEnd();
}
}
obj.lastMove = (new Date()).getTime();
}