forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimate.js
More file actions
344 lines (303 loc) · 9.68 KB
/
animate.js
File metadata and controls
344 lines (303 loc) · 9.68 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
steal('jquery', function ($) {
// Overwrites `jQuery.fn.animate` to use CSS 3 animations if possible
var
// The global animation counter
animationNum = 0,
// The stylesheet for our animations
styleSheet = null,
// The animation cache
cache = [],
// Stores the browser properties like transition end event name and prefix
browser = null,
// Store the original $.fn.animate
oldanimate = $.fn.animate,
// Return the stylesheet, create it if it doesn't exists
getStyleSheet = 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 $.fn.animate.
passThrough = function (props, ops, easing, callback) {
var nonElement = !(this[0] && this[0].nodeType),
isInline = !nonElement && $(this).css("display") === "inline" && $(this).css("float") === "none",
browser = getBrowser();
for (var name in props) {
// jQuery does something with these values
if (props[name] == 'show' || props[name] == 'hide' || props[name] == 'toggle'
// Arrays for individual easing
|| $.isArray(props[name])
// Negative values not handled the same
|| props[name] < 0
// unit-less value
|| name == 'zIndex' || name == 'z-index' || name == 'scrollTop' || name == 'scrollLeft'
) {
return true;
}
}
return props.jquery === true || browser === null || browser.prefix === '-o-' ||
// Animating empty properties
$.isEmptyObject(props) ||
// We can't do custom easing
(easing || easing && typeof easing == 'string') ||
// Second parameter is an object - we can only handle primitives
$.isPlainObject(ops) ||
// Inline and non elements
isInline || nonElement;
},
// Gets a CSS number (with px added as the default unit if the value is a number)
cssValue = function(origName, value) {
if (typeof value === "number" && !$.cssNumber[ origName ]) {
return value += "px";
}
return value;
},
// Feature detection borrowed by http://modernizr.com/
getBrowser = function(){
if(!browser) {
var t,
el = document.createElement('fakeelement'),
transitions = {
'transition': {
transitionEnd : 'transitionend',
prefix : ''
},
'MozTransition': {
transitionEnd : 'animationend',
prefix : '-moz-'
},
'WebkitTransition': {
transitionEnd : 'webkitTransitionEnd',
prefix : '-webkit-'
},
'OTransition': {
transitionEnd : 'oTransitionEnd',
prefix : '-o-'
}
}
for(t in transitions){
if( t in el.style ){
browser = transitions[t];
}
}
}
return browser;
},
// Properties that Firefox can't animate if set to 'auto':
// https://bugzilla.mozilla.org/show_bug.cgi?id=571344
// Provides a converter that returns the actual value
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';
}
},
// Add browser specific prefix
addPrefix = function(properties) {
var result = {};
$.each(properties, function(name, value) {
result[getBrowser().prefix + name] = value;
});
return result;
},
// 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 sheet, 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
sheet = getStyleSheet();
name = "jquerypp_animation_" + (animationNum++);
// get the last sheet and insert this rule into it
sheet.insertRule("@" + getBrowser().prefix + "keyframes " + name + ' ' + style,
(sheet.cssRules && sheet.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(sheet, last.name);
}
}
return name;
};
/**
* @parent jQuery.animate
* @function jQuery.fn.animate
* @signature $(element).animate(options)
* @hide
*
* Animate CSS properties using native CSS animations, if possible.
* Uses the original [$.fn.animate()](http://api.$.com/animate/) otherwise.
*
* @param {Object} props The CSS properties to animate
* @param {Integer|String|Object} [speed=400] The animation duration in ms.
* Will use $.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
*/
$.fn.animate = function (props, speed, easing, callback) {
//default to normal animations if browser doesn't support them
if (passThrough.apply(this, arguments)) {
return oldanimate.apply(this, arguments);
}
var optall = $.speed(speed, easing, callback),
overflow = [];
// if we are animating height and width properties, set overflow to hidden, and save
// the previous overflow information to replace with when done.
if("height" in props || "width" in props) {
overflow = [this[0].style.overflow, this[0].style.overflowX, this[0].style.overflowY];
this.css('overflow', 'hidden');
}
// Add everything to the animation queue
this.queue(optall.queue, function(done) {
var
//current CSS values
current,
// The list of properties passed
properties = [],
to = "",
prop,
self = $(this),
duration = optall.duration,
//the animation keyframe name
animationName,
// The key used to store the animation hook
dataKey,
//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) {
// As long as we don't stop mid animation, then we will replace
// the overflow values of the element being animated.
if(!exec) {
self[0].style.overflow = overflow[0];
self[0].style.overflowX = overflow[1];
self[0].style.overflowY = overflow[2];
}
else {
self.css('overflow', '');
}
self.css(currentCSS);
self.css(addPrefix({
"animation-duration" : "",
"animation-name" : "",
"animation-fill-mode" : "",
"animation-play-state" : ""
}));
// Call the original callback
if ($.isFunction(optall.old) && exec) {
// Call success, pass the DOM element as the this reference
optall.old.call(self[0], true)
}
$.removeData(self, dataKey, true);
},
finishAnimation = function() {
// Call animationEnd using the passed properties
animationEnd(props, true);
done();
};
for(prop in props) {
properties.push(prop);
}
if(getBrowser().prefix === '-moz-' || /Edge\/\d+/.test(navigator.userAgent)) {
// Normalize 'auto' properties in FF
// This is also needed in Edge (tested in 13)
$.each(properties, function(i, prop) {
var converter = ffProps[$.camelCase(prop)];
if(converter && self.css(prop) == 'auto') {
self.css(prop, converter(self));
}
});
}
// Use $.styles
current = self.css.apply(self, properties);
$.each(properties, function(i, cur) {
// Convert a camelcased property name
var name = cur.replace(/([A-Z]|^ms)/g, "-$1" ).toLowerCase();
style += name + " : " + cssValue(cur, current[cur]) + "; ";
to += name + " : " + cssValue(cur, props[cur]) + "; ";
});
style += "} to {" + to + " }}";
animationName = getAnimation(style);
dataKey = animationName + '.run';
// Add a hook which will be called when the animation stops
$._data(this, dataKey, {
stop : function(gotoEnd) {
// Pause the animation
self.css(addPrefix({
'animation-play-state' : 'paused'
}));
// Unbind the animation end handler
self.off(getBrowser().transitionEnd, finishAnimation);
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"
}));
// Attach the transition end event handler to run only once
self.one(getBrowser().transitionEnd, finishAnimation);
});
return this;
};
return $;
});