-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssanimations.js
More file actions
360 lines (302 loc) · 12.6 KB
/
Copy pathcssanimations.js
File metadata and controls
360 lines (302 loc) · 12.6 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
/*
* CSS animations plugin 1.01
* Copyright (c) 2019 Adam Lafene
* https://github.com/EdamL/css-animation
*
* Licensed under the terms of the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function (window) {
'use strict';
//
// Variables
//
var supports = !!document.querySelector; // Feature test
var $ = window.jQuery;
//////////////////////////////////////
// HELPER FUNCTIONS
//////////////////////////////////////
/**
* A simple forEach() implementation for Arrays, Objects and NodeLists
* @private
* @param {Array|Object|NodeList} collection Collection of items to iterate
* @param {Function} callback Callback function for each iteration
* @param {Array|Object|NodeList} scope Object/NodeList/Array that forEach is iterating over (aka `this`)
*/
var forEach = function (collection, callback, scope) {
if (Object.prototype.toString.call(collection) === '[object Object]') {
for (var prop in collection) {
if (Object.prototype.hasOwnProperty.call(collection, prop)) {
callback.call(scope, collection[prop], prop, collection);
}
}
} else {
for (var i = 0, len = collection.length; i < len; i++) {
callback.call(scope, collection[i], i, collection);
}
}
};
/**
* To set multiple style properties on either a single DOM object or multiple DOM objects:
* (pass properties in as an object of property/value pairs)
*/
var setProperties = function(objArray, properties) {
var setProp = function(obj, prop, val) {
obj.style[prop] = val;
}
if(objArray.length) {
forEach(objArray, function (obj) {
for (var property in properties)
setProp(obj, property, properties[property]);
});
}
else {
for (var property in properties)
setProp(objArray, property, properties[property]);
}
return objArray;
};
/**
* Get default (pre-styled) CSS property for a DOM object
*/
function getDefaultProperty(obj, property) {
var nodeName = obj.nodeName;
var temp = document.body.appendChild( document.createElement( nodeName ) );
var prop = window.getComputedStyle(temp).getPropertyValue(property);
temp.parentNode.removeChild( temp );
return prop;
}
//////////////////////////////////////
// prototype
//////////////////////////////////////
/**
* For non-jQuery implementation use 'CssAni' namespace
*/
if (!$) {
var CssAni = function(selector) {
if (!(this instanceof CssAni))
return new CssAni(selector);
this.domObj = document.querySelectorAll(selector);
};
CssAni.fn = CssAni.prototype = {};
window.CssAni = CssAni;
$ = window.CssAni;
}
/**
* cssSlideToggle public method
*/
$.fn.cssSlideToggle = function(param1, param2, param3) {
var domObj = this.domObj || this;
if (domObj.length < 1 || !supports)
return false;
var callback = ((param1 && typeof param1 === "function") ? param1 :
(param2 && typeof param2 === "function") ? param2 :
(param3 && typeof param3 === "function") ? param3 : '');
var duration = (param1 && typeof param1 !== "function") ? parseInt(param1) : 500;
var easing = (param2 && typeof param2 !== "function") ? param2 : 'ease';
forEach(domObj, function(obj, i) {
// for iterating through jQuery objects
if (obj.nodeType !== 1 || isNaN(i))
return;
var objStyle = window.getComputedStyle(obj);
var display = objStyle.getPropertyValue('display');
var isBorderBox = objStyle.getPropertyValue('box-sizing') == 'border-box' ? true : false;
var objHeight, endHeight, endPaddingTop, endPaddingBottom, endMarginTop, endMarginBottom;
var objIsVisible;
// setup initial properties
if (display === 'none') {
// remove padding and margin - position absolute and set opacity to 0 so we can get height
setProperties(obj, {
'margin-top' : 0,
'margin-bottom' : 0,
'overflow' : 'hidden',
'display' : '',
'opacity' : 0,
'position' : 'absolute'
});
// if element is set to {display : none} in the stylesheet, set to default display value for that node type
if (objStyle.getPropertyValue('display') === 'none')
obj.style.display = getDefaultProperty(obj, 'display');
//get default obj height
objHeight = obj.offsetHeight;
// reset opacity and set obj height to 0
setProperties(obj, {
'padding-top' : 0,
'padding-bottom' : 0,
'max-height': 0,
'opacity' : '',
'position' : ''
});
endHeight = objHeight;
endPaddingTop = '';
endPaddingBottom = '';
endMarginTop = '';
endMarginBottom = '';
}
else {
objIsVisible = true;
//get default obj height minus top and bottom padding
objHeight = obj.offsetHeight;
if (!isBorderBox)
objHeight = objHeight - (parseInt(objStyle.getPropertyValue('padding-top')) - parseInt(objStyle.getPropertyValue('padding-bottom')));
setProperties(obj, {
'max-height': objHeight + 'px'
});
endHeight = 0;
endPaddingTop = 0;
endPaddingBottom = 0;
endMarginTop = 0;
endMarginBottom = 0;
}
// do the animation
setTimeout(function() {
setProperties(obj, {
'transition' : 'all '+duration+'ms '+easing,
'-webkit-transition' : 'all '+duration+'ms '+easing,
'-moz-transition' : 'all '+duration+'ms '+easing,
'-ms-transition' : 'all '+duration+'ms '+easing,
'-o-transition' : 'all '+duration+'ms '+easing
});
setTimeout(function() {
setProperties(obj, {
'max-height' : endHeight + 'px',
'padding-top' : endPaddingTop,
'padding-bottom' : endPaddingBottom,
'margin-top' : endMarginTop,
'margin-bottom' : endMarginBottom,
'overflow' : 'hidden'
});
setTimeout(function() {
setProperties(obj, {
'transition' : '',
'-webkit-transition' : '',
'-moz-transition' : '',
'-ms-transition' : '',
'-o-transition' : ''
});
if (objIsVisible)
obj.style.display = 'none';
setProperties(obj, {
'max-height' : '',
'padding-top' : '',
'padding-bottom' : '',
'margin-top' : '',
'margin-bottom' : '',
'overflow' : ''
});
if (callback && i==0){
callback.call(domObj);
}
}, duration+50);
}, 50);
}, 50);
});
};
/**
* cssFadeToggle public method
*/
$.fn.cssFadeToggle = function(param1, param2, param3) {
var domObj = this.domObj || this;
if (domObj.length < 1 || !supports)
return false;
var callback = ((param1 && typeof param1 === "function") ? param1 :
(param2 && typeof param2 === "function") ? param2 :
(param3 && typeof param3 === "function") ? param3 : '');
var duration = (param1 && typeof param1 !== "function") ? parseInt(param1) : 500;
var easing = (param2 && typeof param2 !== "function") ? param2 : 'ease';
forEach(domObj, function(obj, i) {
// for iterating through jQuery objects
if (obj.nodeType !== 1 || isNaN(i))
return;
var objStyle = window.getComputedStyle(obj);
var display = objStyle.getPropertyValue('display');
var objIsVisible;
// setup initial properties
if (display === 'none') {
setProperties(obj, {
'opacity' : 0,
'display' : ''
});
// if element is set to {display : none} in the stylesheet, set to default display value for that node type
if (objStyle.getPropertyValue('display') === 'none')
obj.style.display = getDefaultProperty(obj, 'display');
}
else {
objIsVisible = true;
}
// do the animation
setProperties(obj, {
'transition' : 'all '+duration+'ms '+easing,
'-webkit-transition' : 'all '+duration+'ms '+easing,
'-moz-transition' : 'all '+duration+'ms '+easing,
'-ms-transition' : 'all '+duration+'ms '+easing,
'-o-transition' : 'all '+duration+'ms '+easing
});
setTimeout(function() {
setProperties(obj, {
'opacity' : (objIsVisible ? 0 : 1)
});
setTimeout(function() {
if (objIsVisible)
obj.style.display = 'none';
setProperties(obj, {
'transition' : '',
'-webkit-transition' : '',
'-moz-transition' : '',
'-ms-transition' : '',
'-o-transition' : '',
'opacity' : ''
});
if (callback && i==0){
callback.call(domObj);
}
}, duration+50);
}, 50);
});
};
/**
* cssAnimate public method
*/
$.fn.cssAnimate = function(param1, param2, param3, param4) {
var domObj = this.domObj || this;
if (domObj.length < 1 || !supports)
return false;
if (!param1 || typeof(param1) !== 'object') {
return;
}
var callback = ((param2 && typeof param2 === "function") ? param2 :
(param3 && typeof param3 === "function") ? param3 :
(param4 && typeof param4 === "function") ? param4 : ''),
duration = (param2 && typeof param2 !== "function") ? parseInt(param2) : 500,
easing = (param3 && typeof param3 !== "function") ? param3 : 'ease';
forEach(domObj, function(obj, i) {
// for iterating through jQuery objects
if (obj.nodeType !== 1 || isNaN(i))
return;
// do the animation
setProperties(obj, {
'transition' : 'all '+duration+'ms '+easing,
'-webkit-transition' : 'all '+duration+'ms '+easing,
'-moz-transition' : 'all '+duration+'ms '+easing,
'-ms-transition' : 'all '+duration+'ms '+easing,
'-o-transition' : 'all '+duration+'ms '+easing
});
setTimeout(function() {
setProperties(obj, param1);
setTimeout(function() {
setProperties(obj, {
'transition' : '',
'-webkit-transition' : '',
'-moz-transition' : '',
'-ms-transition' : '',
'-o-transition' : ''
});
if (callback && i==0) {
callback.call(domObj);
}
}, duration+50);
}, 50);
});
};
})(window);