forked from shrekshrek/csstween
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsstween.js
More file actions
496 lines (425 loc) · 16.3 KB
/
csstween.js
File metadata and controls
496 lines (425 loc) · 16.3 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*!
* VERSION: 0.6.0
* DATE: 2015-12-31
* GIT:https://github.com/shrekshrek/csstween
*
* @author: Shrek.wang, shrekshrek@gmail.com
**/
(function (factory) {
var root = (typeof self == 'object' && self.self == self && self) ||
(typeof global == 'object' && global.global == global && global);
if (typeof define === 'function' && define.amd) {
define(['exports'], function (exports) {
root.CT = factory(root, exports);
});
} else if (typeof exports !== 'undefined') {
factory(root, exports);
} else {
root.CT = factory(root, {});
}
}(function (root, CT) {
// --------------------------------------------------------------------辅助方法
function extend(obj, obj2) {
for (var prop in obj2) {
obj[prop] = obj2[prop];
}
}
function each(obj, callback) {
if (typeof(obj) === 'function') {
callback.call(obj, 0, obj);
} else if (obj.length === undefined) {
callback.call(obj, 0, obj);
} else {
for (var i = 0; i < obj.length; i++) {
callback.call(obj[i], i, obj[i]);
}
}
}
// WebkitTransform 转 -webkit-transform
function hyphenize(str) {
return str.replace(/([A-Z])/g, "-$1").toLowerCase();
}
// transformOrigin 转 TransformOrigin
function firstUpper(str) {
return str.replace(/\b(\w)|\s(\w)/g, function (m) {
return m.toUpperCase();
});
}
// --------------------------------------------------------------------检测是否支持,浏览器补全方法
var prefix = '';
var A_START_EVENT = 'animationstart';
var A_REPEAT_EVENT = 'animationiteration';
var A_END_EVENT = 'animationend';
var T_END_EVENT = 'transitionend';
(function () {
var _d = document.createElement('div');
var _prefixes = ['Webkit', 'Moz', 'Ms', 'O'];
for (var i in _prefixes) {
if ((_prefixes[i] + 'Transition') in _d.style) {
prefix = _prefixes[i];
if (prefix !== 'Moz') {
A_START_EVENT = prefix.toLowerCase() + 'AnimationStart';
A_REPEAT_EVENT = prefix.toLowerCase() + 'AnimationIteration';
A_END_EVENT = prefix.toLowerCase() + 'AnimationEnd';
T_END_EVENT = prefix.toLowerCase() + 'TransitionEnd';
}
break;
}
}
}());
function browserPrefix(str) {
if (str) {
return prefix + firstUpper(str);
} else {
return prefix;
}
}
var requestFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame;
// --------------------------------------------------------------------style 相关函数
function getElement(target) {
if (!target) throw "target is undefined, can't tween!!!";
if (typeof(target) == 'string') {
return (typeof(document) === 'undefined') ? target : (document.querySelectorAll ? document.querySelectorAll(target) : document.getElementById((target.charAt(0) === '#') ? target.substr(1) : target));
} else {
return target;
}
}
function checkCssName(target, name) {
if (target.style[name] !== undefined) return name;
name = browserPrefix(name);
if (target.style[name] !== undefined) return name;
return undefined;
}
function calcValue(value, value2) {
if (typeof(value2) === 'string') {
var _s = value2.substr(0, 2);
var _n = parseFloat(value2.substr(2));
switch (_s) {
case '+=':
value2 = parseFloat(value) + _n;
break;
case '-=':
value2 = parseFloat(value) - _n;
break;
}
}
return value2;
}
function checkCssValue(name, value) {
switch (name) {
case 'opacity':
case 'fontWeight':
case 'lineHeight':
case 'zoom':
return value;
default:
return typeof(value) === 'number' ? Math.round(value) + 'px' : value;
break;
}
}
function getStyle(target, name) {
//if (target.style[name]) {
// return target.style[name];
//} else
if (document.defaultView && document.defaultView.getComputedStyle) {
var _p = hyphenize(name);
var _s = document.defaultView.getComputedStyle(target, '');
return _s && _s.getPropertyValue(_p);
} else if (target.currentStyle) {
return target.currentStyle[name];
} else {
return null;
}
}
function setStyle(target, params) {
for (var i in params) {
if(target.style[i] != undefined) target.style[i] = checkCssValue(i, params[i]);
}
}
// --------------------------------------------------------------------css rule 相关函数
var keyframesRule = window.CSSRule.KEYFRAMES_RULE || window.CSSRule.WEBKIT_KEYFRAMES_RULE || window.CSSRule.MOZ_KEYFRAMES_RULE;
var styleRule = window.CSSRule.STYLE_RULE || window.CSSRule.WEBKIT_STYLE_RULE || window.CSSRule.MOZ_STYLE_RULE;
var ctSheet;
var ctRules;
var ruleId = 0;
(function () {
var _style = document.createElement('style');
_style.rel = 'stylesheet';
_style.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(_style);
ctSheet = _style.sheet;
ctRules = ctSheet.cssRules || ctSheet.rules || [];
}());
function createRuleId() {
return ++ruleId;
}
function getRule(name) {
for (var i in ctRules) {
var _rule = ctRules[i];
if ((_rule.type === keyframesRule && _rule.name === name) || (_rule.type === styleRule && _rule.selectorText === name)) {
return {rule: _rule, index: i};
}
}
return null;
}
function addRule(name, content) {
var _index = ctRules.length;
if (ctSheet.insertRule) {
ctSheet.insertRule(name + '{' + content + '}', _index);
} else if (ctSheet.addRule) {
ctSheet.addRule(name, content, _index);
}
}
function removeRule(name) {
var _obj = getRule(name);
if (_obj === null) return;
if (ctSheet.deleteRule) {
ctSheet.deleteRule(_obj.index);
} else if (ctSheet.removeRule) {
ctSheet.removeRule(_obj.index);
}
}
function addKfsRule(name, fromVars, toVars) {
var _name = 'ct_kfs_' + name;
var _txt1 = '0%{';
var _txt2 = '100%{';
for (var i in fromVars) {
_txt1 += hyphenize(i) + ':' + checkCssValue(i, fromVars[i]) + ';';
_txt2 += hyphenize(i) + ':' + checkCssValue(i, toVars[i]) + ';';
}
_txt1 += '}';
_txt2 += '}';
addRule('@' + hyphenize(browserPrefix('Keyframes')) + ' ' + _name, _txt1 + _txt2);
return _name;
}
// --------------------------------------------------------------------tween
var tweens = {};
var tweenId = 0;
function createTweenId() {
return ++tweenId;
}
function tween() {
this.initialize.apply(this, arguments);
}
extend(tween.prototype, {
initialize: function (target, time, fromVars, toVars) {
var _self = this;
this.fromVars = fromVars;
this.toVars = toVars;
this.target = target;
this.duration = Math.max(time, 0);
this.ease = 'cubic-bezier' + (toVars.ease || CT.Linear.None);
this.delay = Math.max(toVars.delay || 0, 0);
this.yoyo = toVars.yoyo || false;
this.repeat = Math.floor(toVars.repeat || 1);
this.onStart = toVars.onStart || null;
this.onStartParams = toVars.onStartParams || [];
this.onRepeat = toVars.onRepeat || null;
this.onRepeatParams = toVars.onRepeatParams || [];
this.onEnd = toVars.onEnd || null;
this.onEndParams = toVars.onEndParams || [];
this.type = toVars.type || '';
var _tid = createTweenId();
if (this.target._ct_id) {
tweens[this.target._ct_id].destroy();
}
this.target._ct_id = _tid;
if (this.type == 'a') {
this.startHandler = function () {
if (_self.onStart) _self.onStart.apply(this, _self.onStartParams);
};
this.target.addEventListener(A_START_EVENT, this.startHandler, false);
this.repeatHandler = function () {
if (_self.onRepeat) _self.onRepeat.apply(this, _self.onRepeatParams);
};
this.target.addEventListener(A_REPEAT_EVENT, this.repeatHandler, false);
this.endHandler = function () {
_self.destroy(true);
};
this.target.addEventListener(A_END_EVENT, this.endHandler, false);
var _rid = createRuleId();
this.kfsName = addKfsRule(_rid, this.fromVars, this.toVars);
this.target.style[browserPrefix('Animation')] = this.kfsName + ' ' + this.duration + 's ' + this.ease + ' ' + this.delay + 's ' + (this.repeat < 0 ? 'infinite' : this.repeat) + ' ' + (this.yoyo ? 'alternate' : 'normal');
this.target.style[browserPrefix('AnimationFillMode')] = 'both';
setStyle(this.target, this.toVars);
} else {
this.endHandler = function () {
_self.destroy(true);
};
this.target.addEventListener(T_END_EVENT, this.endHandler, false);
requestFrame(function () {
requestFrame(function () {
_self.target.style[browserPrefix('Transition')] = 'all ' + _self.duration + 's ' + _self.ease + ' ' + _self.delay + 's';
setStyle(_self.target, _self.toVars);console.log(_self.target, _self.toVars);
});
});
}
tweens[_tid] = this;
},
destroy: function (end) {
if (end == false) {
for (var i in this.toVars) {
if (this.target.style[i] != undefined) this.target.style[i] = getStyle(this.target, i);
}
}
if (this.type == 'a') {
this.target.removeEventListener(A_START_EVENT, this.startHandler, false);
this.target.removeEventListener(A_REPEAT_EVENT, this.repeatHandler, false);
this.target.removeEventListener(A_END_EVENT, this.endHandler, false);
this.target.style[browserPrefix('Animation')] = '';
this.target.style[browserPrefix('AnimationFillMode')] = '';
removeRule(this.kfsName);
} else {
this.target.removeEventListener(T_END_EVENT, this.endHandler, false);
this.target.style[browserPrefix('Transition')] = '';
}
delete tweens[this.target._ct_id];
delete this.target._ct_id;
if (end == true) {
if (this.onEnd) this.onEnd.apply(this, this.onEndParams);
}
}
});
// --------------------------------------------------------------------主要方法
extend(CT, {
get: function (target, param) {
var _target = getElement(target);
if (_target.length !== undefined) {
_target = _target[0];
}
var _name = checkCssName(_target, param);
if (_name)
return getStyle(_target, _name);
else
return null;
},
set: function (target, params) {
var _target = getElement(target);
each(_target, function (index, obj) {
var _params = {};
for (var j in params) {
var _name = checkCssName(obj, j);
if (_name) {
_params[_name] = checkCssValue(_name, calcValue(getStyle(obj, _name), params[j]));
}
}
setStyle(obj, _params);
});
},
fromTo: function (target, time, fromVars, toVars) {
var _target = getElement(target);
var _tweens = [];
each(_target, function (index, obj) {
var _fromVars = {};
var _toVars = {};
for (var j in toVars) {
var _name = checkCssName(obj, j);
if (_name) {
var _n = getStyle(obj, _name);
_fromVars[_name] = calcValue(_n, fromVars[j]);
_toVars[_name] = calcValue(_n, toVars[j]);
} else {
_toVars[j] = toVars[j];
}
}
setStyle(obj, _fromVars);
var _tween = new tween(obj, time, _fromVars, _toVars);
_tweens.push(_tween);
});
if (_tweens.length == 1) {
return _tweens[0];
} else {
return _tweens;
}
},
from: function (target, time, fromVars) {
var _target = getElement(target);
var _tweens = [];
each(_target, function (index, obj) {
var _fromVars = {};
var _toVars = {};
for (var j in fromVars) {
var _name = checkCssName(obj, j);
if (_name) {
var _n = getStyle(obj, _name);
_toVars[_name] = _n;
_fromVars[_name] = calcValue(_n, fromVars[j]);
} else {
_toVars[j] = fromVars[j];
}
}
setStyle(obj, _fromVars);
var _tween = new tween(obj, time, _fromVars, _toVars);
_tweens.push(_tween);
});
if (_tweens.length == 1) {
return _tweens[0];
} else {
return _tweens;
}
},
to: function (target, time, toVars) {
var _target = getElement(target);
var _tweens = [];
each(_target, function (index, obj) {
var _fromVars = {};
var _toVars = {};
for (var j in toVars) {
var _name = checkCssName(obj, j);
if (_name) {
var _n = getStyle(obj, _name);
_fromVars[_name] = _n;
_toVars[_name] = calcValue(_n, toVars[j]);
} else {
_toVars[j] = toVars[j];
}
}
var _tween = new tween(obj, time, _fromVars, _toVars);
_tweens.push(_tween);
});
if (_tweens.length == 1) {
return _tweens[0];
} else {
return _tweens;
}
},
kill: function (target, end) {
var _target = getElement(target);
each(_target, function (index, obj) {
if (obj._ct_id) {
tweens[obj._ct_id].destroy(end);
}
});
},
killAll: function (end) {
for (var i in tweens) {
tweens[i].destroy(end);
}
}
});
// --------------------------------------------------------------------缓动选项
extend(CT, {
Linear: {
None: '(0, 0, 1, 1)'
},
Quad: {
In: '(0.3, 0, 0.65, 0.75)',
Out: '(0.35, 0.25, 0.7, 1)',
InOut: '(0.46, 0.03, 0.54, 0.97)'
},
Quart: {
In: '(0.5, 0, 0.75, 0)',
Out: '(0.25, 1, 0.5, 1)',
InOut: '(0.75, 0, 0.25, 1)'
},
Back: {
In: '(0, 0.35, 0.7, -0.6)',
Out: '(0.3, 1.6, 0.65, 1)'
}
});
return CT;
}));