-
Notifications
You must be signed in to change notification settings - Fork 7
/
move.js
167 lines (163 loc) · 4.49 KB
/
move.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
缓冲运动,弹性运动
coefficient越大越慢
Copyright 2014 03 15 by Ada Wang/汪洁文
*/
(function($){
var bufferMoves = [];
$.fn.bufferMove = function(oTarget,coefficient,fnCallBack){
return this.each(function() {
bufferMoves.push(new $.bufferMove($(this),oTarget,coefficient,fnCallBack));
});
};
$.bufferMove = function(obj,oTarget,coefficient,fnCallBack) {
this.obj = obj;
this.oTarget = oTarget;
this.coefficient = coefficient;
this.callback = fnCallBack;
this.timer = null;
this.bufferMoving();
};
$.bufferMove.fn = $.bufferMove.prototype = {};
$.bufferMove.fn.extend = $.extend;
$.bufferMove.fn.extend({
/*缓冲运动*/
bufferMoving : function(){
var _this = this;
var iInterval = 10;
var oSpeed = {};
if(this.timer) clearInterval(_this.timer);
this.timer=setInterval(function(){
var oTmp = {};
for(attr in _this.oTarget){
oTmp[attr] = parseFloat(_this.obj.css(attr));
oTmp[attr] = (_this.oTarget[attr]-oTmp[attr])>0? Math.ceil(oTmp[attr]):Math.floor(oTmp[attr]);
oSpeed[attr] = (_this.oTarget[attr] - oTmp[attr])/_this.coefficient;
oSpeed[attr] = oSpeed[attr] > 0 ? Math.ceil(oSpeed[attr]):Math.floor(oSpeed[attr]);
}
_this.bufferdoMove(oTmp,oSpeed);
},iInterval)
},
bufferdoMove:function(oTmp, oSpeed)
{
var bStop = true;
var _this = this;
for(attr in oTmp){
//以前这里有问题,一直执行这个,系统一直认为这个大于1,因为oTmp[attr]
if(Math.abs(_this.oTarget[attr]-oTmp[attr])>1 || Math.abs(oSpeed[attr])>1){
bStop = false;
console.log(5);
}
}
if(bStop)
{
clearInterval(_this.timer);
_this.timer=null;
_this.obj.css(_this.oTarget);
if(this.callback) this.callback();
}
else
{
for(attr in oSpeed){
oTmp[attr]+=oSpeed[attr];
}
_this.obj.css(oTmp);
}
}
});
})(jQuery);
/*弹性运动*/
(function($){
var moveArray = [];
$.fn.flexibleMove = function(oTarget,coefficient,fnCallBack){
return this.each(function(i,elem) {
$(elem).attr('flexibleMove-index',i);
moveArray.push(new $.flexibleMove($(this),oTarget,coefficient,fnCallBack));
});
};
$.fn.flexibleStop = function(){
if (moveArray.length==0) return this;
var index = $(this).attr('flexibleMove-index');
if (moveArray.length < index+1) {
return this;
};
var obj = moveArray[index];
if(obj!=null && typeof obj.timer!= "undefined"){
moveArray.splice(index,1);
clearInterval(obj.timer);
}
return this;
}
$.flexibleMove = function(obj,oTarget,coefficient,fnCallBack) {
this.obj = obj;
this.oTarget = oTarget;
this.coefficient = coefficient;
this.callback = fnCallBack;
this.timer = null;
this.init();
};
var debug = function(msg){
console.log(msg);
}
$.flexibleMove.fn = $.flexibleMove.prototype = {};
$.flexibleMove.fn.extend = $.extend;
$.flexibleMove.fn.extend({
init : function(){
var iInterval = 10;
var oSpeed = {};
var maxSpeed=65;
var _this = this;
// if(typeof this.timer=='undefined') this.timer = null;
if(_this.timer != null) {
// debug('begin2-------------------------------');
debug(_this.timer);
clearInterval(_this.timer)
debug(_this.timer);
// debug('end---------------------------------');
};
for(attr in _this.oTarget){
oSpeed[attr] = 0;
}
this.timer = setInterval(function(){
var oTmp = {};
for(attr in _this.oTarget){
oTmp[attr] = parseFloat(_this.obj.css(attr));
oTmp[attr] = (_this.oTarget[attr]-oTmp[attr])>0? Math.ceil(oTmp[attr]):Math.floor(oTmp[attr]);
oSpeed[attr]+=(_this.oTarget[attr]-oTmp[attr])/_this.coefficient;
oSpeed[attr]*=0.8;//不乘以这个的话,运动不会停,速度会在正的最大值和负的最大值间变化
if(Math.abs(oSpeed[attr])>maxSpeed)
{
oSpeed[attr]=oSpeed[attr]>0?maxSpeed:-maxSpeed;
}
};
_this.bufferdoMove(oTmp, oSpeed);
},iInterval);
},
bufferdoMove:function(oTmp, oSpeed)
{
var bStop = true;
var _this = this;
for(attr in oTmp){
//以前这里有问题,一直执行这个,系统一直认为这个大于1,因为oTmp[attr]
if(Math.abs(_this.oTarget[attr]-oTmp[attr])>2|| Math.abs(oSpeed[attr])>2){
bStop = false;
console.log(5);
}
}
if(bStop)
{
clearInterval(_this.timer);
_this.timer=null;
_this.obj.css(_this.oTarget);
if(this.callback) this.callback();
}
else
{
for(attr in oSpeed){
oTmp[attr]+=oSpeed[attr];
}
_this.obj.css(oTmp);
}
}
});
})(jQuery);