forked from bitovi/jquerypp
/
animate.js
310 lines (271 loc) · 8.48 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
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
steal('jquery', 'jquery/dom/styles').then(function ($) {
var animationNum = 0,
styleSheet = null,
// Create a new stylesheet where we store our animations
getLastStyleSheet = function () {
if(!styleSheet) {
var style = document.createElement('style');
style.setAttribute("type", "text/css");
style.setAttribute("media", "screen");
document.getElementsByTagName('head')[0].appendChild(style);
if (!window.createPopup) { /* For Safari */
style.appendChild(document.createTextNode(''));
}
styleSheet = style.sheet;
}
return styleSheet;
},
//removes an animation rule from a sheet
removeAnimation = function (sheet, name) {
for (var j = sheet.cssRules.length - 1; j >= 0; j--) {
var rule = sheet.cssRules[j];
// 7 means the keyframe rule
if (rule.type === 7 && rule.name == name) {
sheet.deleteRule(j)
return;
}
}
},
/**
* Returns whether the animation should be passed to the original
* jQuery.fn.animate.
*/
passThrough = function (props, ops) {
var nonElement = !(this[0] && this[0].nodeType),
isInline = !nonElement && $(this).css("display") === "inline" && $(this).css("float") === "none";
for (var name in props) {
if (props[name] == 'show' || props[name] == 'hide' // jQuery does something with these two values
|| jQuery.isArray(props[name]) // Arrays for individual easing
|| props[name] < 0 // Negative values not handled the same
|| name == 'zIndex' || name == 'z-index'
) { // unit-less value
return true;
}
}
return props.jquery === true || browser === null ||
jQuery.isEmptyObject(props) || // Animating empty properties
jQuery.isPlainObject(ops) || // Second parameter is an object - anifast only handles numbers
typeof ops == 'string' || // Second parameter is a string like 'slow' TODO: remove
isInline || nonElement;
},
/**
* Return the CSS number (with px added as the default unit if the value is a number)
*/
cssNumber = function(origName, value) {
if (typeof value === "number" && !jQuery.cssNumber[ origName ]) {
return value += "px";
}
return value;
},
// Feature detection borrowed by http://modernizr.com/
getBrowserProperties = function(){
var t,
el = document.createElement('fakeelement'),
transitions = {
'transition': {
transitionEnd : 'transitionEnd',
prefix : ''
},
'OTransition': {
transitionEnd : 'oTransitionEnd',
prefix : '-o-'
},
// 'MSTransition': {
// transitionEnd : 'msTransitionEnd',
// prefix : '-ms-'
// },
'MozTransition': {
transitionEnd : 'animationend',
prefix : '-moz-'
},
'WebkitTransition': {
transitionEnd : 'webkitAnimationEnd',
prefix : '-webkit-'
}
}
for(t in transitions){
if( el.style[t] !== undefined ){
return transitions[t];
}
}
return null;
},
// Properties that Firefox can't animate if set to 'auto'
// https://bugzilla.mozilla.org/show_bug.cgi?id=571344
ffProps = {
top : function(el) {
return el.position().top;
},
left : function(el) {
return el.position().left;
},
width : function(el) {
return el.width();
},
height : function(el) {
return el.height();
},
fontSize : function(el) {
return '1em';
}
},
browser = getBrowserProperties(),
/**
* Add browser specific prefix
*/
addPrefix = function(properties) {
var result = {};
jQuery.each(properties, function(name, value) {
result[browser.prefix + name] = value;
});
return result;
},
// The animation cache
cache = [],
/**
* Returns the animation name for a given style. It either uses a cached
* version or adds it to the stylesheet, removing the oldest style if the
* cache has reached a certain size.
*/
getAnimation = function(style) {
var lastSheet, name, last;
// Look up the cached style, set it to that name and reset age if found
// increment the age for any other animation
$.each(cache, function(i, animation) {
if(style === animation.style) {
name = animation.name;
animation.age = 0;
} else {
animation.age += 1;
}
});
if(!name) { // Add a new style
lastSheet = getLastStyleSheet();
name = "animate" + (animationNum++);
// get the last sheet and insert this rule into it
lastSheet.insertRule("@" + browser.prefix + "keyframes " + name + ' ' + style,
(lastSheet.cssRules && lastSheet.cssRules.length) || 0);
cache.push({
name : name,
style : style,
age : 0
});
// Sort the cache by age
cache.sort(function(first, second) {
return first.age - second.age;
});
// Remove the last (oldest) item from the cache if it has more than 20 items
if(cache.length > 20) {
last = cache.pop();
removeAnimation(lastSheet, last.name);
}
}
return name;
},
oldanimate = jQuery.fn.animate;
/**
* @function jQuery.fn.animate
* @parent jQuery.animate
*
* Animate CSS properties using native CSS animations, if possible.
* Uses the original [jQuery.fn.animate()](http://api.jquery.com/animate/) otherwise.
*
* @param {Object} props The CSS properties to animate
* @param {Integer|String|Object} [speed=400] The animation duration in ms.
* Will use jQuery.fn.animate if a string or object is passed
* @param {Function} [callback] A callback to execute once the animation is complete
* @return {jQuery} The jQuery element
*/
jQuery.fn.animate = function (props, speed, callback) {
//default to normal animations if browser doesn't support them
if (passThrough.apply(this, arguments)) {
return oldanimate.apply(this, arguments);
}
if (jQuery.isFunction(speed)) {
callback = speed;
}
this.queue('fx', function(done) {
// Add everything to the animation queue
// Most of of these calls need to happen once per element
var current, //current CSS values
properties = [], // The list of properties passed
to = "",
prop,
self = $(this),
duration = jQuery.fx.speeds[speed] || speed || jQuery.fx.speeds._default,
//the animation keyframe name
animationName,
// The key used to store the animation hook
dataKey = animationName + '.run',
//the text for the keyframe
style = "{ from {",
// The animation end event handler.
// Will be called both on animation end and after calling .stop()
animationEnd = function (currentCSS, exec) {
self.css(currentCSS);
self.css(addPrefix({
"animation-duration" : "",
"animation-name" : "",
"animation-fill-mode" : ""
}));
if (callback && exec) {
// Call success, pass the DOM element as the this reference
callback.call(self[0], true)
}
jQuery.removeData(self, dataKey, true);
}
for(prop in props) {
properties.push(prop);
}
if(browser.prefix === '-moz-') {
// Normalize 'auto' properties in FF
$.each(properties, function(i, prop) {
var converter = ffProps[jQuery.camelCase(prop)];
if(converter && self.css(prop) == 'auto') {
self.css(prop, converter(self));
}
});
}
// Use jQuery.styles
current = self.styles.apply(self, properties);
jQuery.each(properties, function(i, cur) {
// Convert a camelcased property name
var name = cur.replace(/([A-Z]|^ms)/g, "-$1" ).toLowerCase();
style += name + " : " + cssNumber(cur, current[cur]) + "; ";
to += name + " : " + cssNumber(cur, props[cur]) + "; ";
});
style += "} to {" + to + " }}";
animationName = getAnimation(style);
// Add a hook which will be called when the animation stops
jQuery._data(this, dataKey, {
stop : function(gotoEnd) {
// Pause the animation
self.css(addPrefix({
'animation-play-state' : 'paused'
}));
// Unbind the animation end handler
self.off(browser.transitionEnd, animationEnd);
if(!gotoEnd) { // We were told not to finish the animation
// Call animationEnd but set the CSS to the current computed style
animationEnd(self.styles.apply(self, properties), false);
} else {
// Finish animaion
animationEnd(props, true);
}
}
});
// set this element to point to that animation
self.css(addPrefix({
"animation-duration" : duration + "ms",
"animation-name" : animationName,
"animation-fill-mode": "forwards"
}));
self.one(browser.transitionEnd, function() {
// Call animationEnd using the current properties
animationEnd(props, true);
done();
});
});
return this;
};
});