1 |
b9383c72
|
Julien Enselme
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
(function($) {
|
20 |
|
|
$.fn.quicksand = function(collection, customOptions) {
|
21 |
|
|
var options = {
|
22 |
|
|
duration : 750,
|
23 |
|
|
easing : 'swing',
|
24 |
|
|
attribute : 'data-id',
|
25 |
|
|
adjustHeight : 'auto',
|
26 |
|
|
|
27 |
|
|
adjustWidth : 'auto',
|
28 |
|
|
|
29 |
|
|
useScaling : false,
|
30 |
|
|
enhancement : function(c) {},
|
31 |
|
|
selector : '> *',
|
32 |
|
|
atomic : false,
|
33 |
|
|
dx : 0,
|
34 |
|
|
dy : 0,
|
35 |
|
|
maxWidth : 0,
|
36 |
|
|
retainExisting : true
|
37 |
|
|
};
|
38 |
|
|
$.extend(options, customOptions);
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
if ($.browser.msie || (typeof ($.fn.scale) == 'undefined')) {
|
42 |
|
|
options.useScaling = false;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
var callbackFunction;
|
46 |
|
|
if (typeof (arguments[1]) == 'function') {
|
47 |
|
|
callbackFunction = arguments[1];
|
48 |
|
|
} else if (typeof (arguments[2] == 'function')) {
|
49 |
|
|
callbackFunction = arguments[2];
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
return this.each(function(i) {
|
53 |
|
|
var val;
|
54 |
|
|
var animationQueue = [];
|
55 |
|
|
|
56 |
|
|
var $collection;
|
57 |
|
|
if (typeof(options.attribute) == 'function') {
|
58 |
|
|
$collection = $(collection);
|
59 |
|
|
} else {
|
60 |
|
|
$collection = $(collection).filter('[' + options.attribute + ']').clone();
|
61 |
|
|
}
|
62 |
|
|
var $sourceParent = $(this);
|
63 |
|
|
var sourceHeight = $(this).css('height');
|
64 |
|
|
var sourceWidth = $(this).css('width');
|
65 |
|
|
var destHeight, destWidth;
|
66 |
|
|
var adjustHeightOnCallback = false;
|
67 |
|
|
var adjustWidthOnCallback = false;
|
68 |
|
|
var offset = $($sourceParent).offset();
|
69 |
|
|
var offsets = [];
|
70 |
|
|
var $source = $(this).find(options.selector);
|
71 |
|
|
var width = $($source).innerWidth();
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
if ($.browser.msie && parseInt($.browser.version, 10) < 7) {
|
75 |
|
|
$sourceParent.html('').append($collection);
|
76 |
|
|
return;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
var postCallbackPerformed = 0;
|
81 |
|
|
var postCallback = function() {
|
82 |
|
|
$(this).css('margin', '').css('position', '').css('top', '').css('left', '').css('opacity', '');
|
83 |
|
|
if (!postCallbackPerformed) {
|
84 |
|
|
postCallbackPerformed = 1;
|
85 |
|
|
|
86 |
|
|
if (!options.atomic) {
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
var $toDelete = $sourceParent.find(options.selector);
|
91 |
|
|
if (!options.retainExisting) {
|
92 |
|
|
$sourceParent.prepend($dest.find(options.selector));
|
93 |
|
|
$toDelete.remove();
|
94 |
|
|
} else {
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
var $keepElements = $([]);
|
99 |
|
|
$dest.find(options.selector).each(function(i) {
|
100 |
|
|
var $matchedElement = $([]);
|
101 |
|
|
if (typeof (options.attribute) == 'function') {
|
102 |
|
|
var val = options.attribute($(this));
|
103 |
|
|
$toDelete.each(function() {
|
104 |
|
|
if (options.attribute(this) == val) {
|
105 |
|
|
$matchedElement = $(this);
|
106 |
|
|
return false;
|
107 |
|
|
}
|
108 |
|
|
});
|
109 |
|
|
} else {
|
110 |
|
|
$matchedElement = $toDelete.filter(
|
111 |
|
|
'[' + options.attribute + '="'+
|
112 |
|
|
$(this).attr(options.attribute) + '"]');
|
113 |
|
|
}
|
114 |
|
|
if ($matchedElement.length > 0) {
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
$keepElements = $keepElements.add($matchedElement);
|
119 |
|
|
if (i === 0) {
|
120 |
|
|
$sourceParent.prepend($matchedElement);
|
121 |
|
|
} else {
|
122 |
|
|
$matchedElement.insertAfter($sourceParent.find(options.selector).get(i - 1));
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
});
|
126 |
|
|
|
127 |
|
|
$toDelete.not($keepElements).remove();
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
if (adjustHeightOnCallback) {
|
131 |
|
|
$sourceParent.css('height', destHeight);
|
132 |
|
|
}
|
133 |
|
|
if (adjustWidthOnCallback) {
|
134 |
|
|
$sourceParent.css('width', sourceWidth);
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
options.enhancement($sourceParent);
|
138 |
|
|
if (typeof callbackFunction == 'function') {
|
139 |
|
|
callbackFunction.call(this);
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
if (false === options.adjustHeight) {
|
144 |
|
|
$sourceParent.css('height', 'auto');
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
if (false === options.adjustWidth) {
|
148 |
|
|
$sourceParent.css('width', 'auto');
|
149 |
|
|
}
|
150 |
|
|
};
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
var $correctionParent = $sourceParent.offsetParent();
|
154 |
|
|
var correctionOffset = $correctionParent.offset();
|
155 |
|
|
if ($correctionParent.css('position') == 'relative') {
|
156 |
|
|
if ($correctionParent.get(0).nodeName.toLowerCase() != 'body') {
|
157 |
|
|
correctionOffset.top += (parseFloat($correctionParent.css('border-top-width')) || 0);
|
158 |
|
|
correctionOffset.left += (parseFloat($correctionParent.css('border-left-width')) || 0);
|
159 |
|
|
}
|
160 |
|
|
} else {
|
161 |
|
|
correctionOffset.top -= (parseFloat($correctionParent.css('border-top-width')) || 0);
|
162 |
|
|
correctionOffset.left -= (parseFloat($correctionParent.css('border-left-width')) || 0);
|
163 |
|
|
correctionOffset.top -= (parseFloat($correctionParent.css('margin-top')) || 0);
|
164 |
|
|
correctionOffset.left -= (parseFloat($correctionParent.css('margin-left')) || 0);
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
if (isNaN(correctionOffset.left)) {
|
169 |
|
|
correctionOffset.left = 0;
|
170 |
|
|
}
|
171 |
|
|
if (isNaN(correctionOffset.top)) {
|
172 |
|
|
correctionOffset.top = 0;
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
correctionOffset.left -= options.dx;
|
176 |
|
|
correctionOffset.top -= options.dy;
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
$sourceParent.css('height', $(this).height());
|
180 |
|
|
$sourceParent.css('width', $(this).width());
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
$source.each(function(i) {
|
184 |
|
|
offsets[i] = $(this).offset();
|
185 |
|
|
});
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
$(this).stop();
|
189 |
|
|
var dx = 0;
|
190 |
|
|
var dy = 0;
|
191 |
|
|
$source.each(function(i) {
|
192 |
|
|
$(this).stop();
|
193 |
|
|
var rawObj = $(this).get(0);
|
194 |
|
|
if (rawObj.style.position == 'absolute') {
|
195 |
|
|
dx = -options.dx;
|
196 |
|
|
dy = -options.dy;
|
197 |
|
|
} else {
|
198 |
|
|
dx = options.dx;
|
199 |
|
|
dy = options.dy;
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
rawObj.style.position = 'absolute';
|
203 |
|
|
rawObj.style.margin = '0';
|
204 |
|
|
|
205 |
|
|
if (!options.adjustWidth) {
|
206 |
|
|
rawObj.style.width = (width + 'px');
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
rawObj.style.top = (offsets[i].top- parseFloat(rawObj.style.marginTop) - correctionOffset.top + dy) + 'px';
|
212 |
|
|
rawObj.style.left = (offsets[i].left- parseFloat(rawObj.style.marginLeft) - correctionOffset.left + dx) + 'px';
|
213 |
|
|
|
214 |
|
|
if (options.maxWidth > 0 && offsets[i].left > options.maxWidth) {
|
215 |
|
|
rawObj.style.display = 'none';
|
216 |
|
|
}
|
217 |
|
|
});
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
var $dest = $($sourceParent).clone();
|
221 |
|
|
var rawDest = $dest.get(0);
|
222 |
|
|
rawDest.innerHTML = '';
|
223 |
|
|
rawDest.setAttribute('id', '');
|
224 |
|
|
rawDest.style.height = 'auto';
|
225 |
|
|
rawDest.style.width = $sourceParent.width() + 'px';
|
226 |
|
|
$dest.append($collection);
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
$dest.insertBefore($sourceParent);
|
231 |
|
|
$dest.css('opacity', 0.0);
|
232 |
|
|
rawDest.style.zIndex = -1;
|
233 |
|
|
|
234 |
|
|
rawDest.style.margin = '0';
|
235 |
|
|
rawDest.style.position = 'absolute';
|
236 |
|
|
rawDest.style.top = offset.top - correctionOffset.top + 'px';
|
237 |
|
|
rawDest.style.left = offset.left - correctionOffset.left + 'px';
|
238 |
|
|
|
239 |
|
|
if (options.adjustHeight === 'dynamic') {
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
$sourceParent.animate({ height : $dest.height() }, options.duration, options.easing);
|
243 |
|
|
} else if (options.adjustHeight === 'auto') {
|
244 |
|
|
destHeight = $dest.height();
|
245 |
|
|
if (parseFloat(sourceHeight) < parseFloat(destHeight)) {
|
246 |
|
|
|
247 |
|
|
$sourceParent.css('height', destHeight);
|
248 |
|
|
} else {
|
249 |
|
|
|
250 |
|
|
adjustHeightOnCallback = true;
|
251 |
|
|
}
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
if (options.adjustWidth === 'dynamic') {
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
$sourceParent.animate({ width : $dest.width() }, options.duration, options.easing);
|
258 |
|
|
} else if (options.adjustWidth === 'auto') {
|
259 |
|
|
destWidth = $dest.width();
|
260 |
|
|
if (parseFloat(sourceWidth) < parseFloat(destWidth)) {
|
261 |
|
|
|
262 |
|
|
$sourceParent.css('width', destWidth);
|
263 |
|
|
} else {
|
264 |
|
|
|
265 |
|
|
adjustWidthOnCallback = true;
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
$source.each(function(i) {
|
272 |
|
|
var destElement = [];
|
273 |
|
|
if (typeof (options.attribute) == 'function') {
|
274 |
|
|
val = options.attribute($(this));
|
275 |
|
|
$collection.each(function() {
|
276 |
|
|
if (options.attribute(this) == val) {
|
277 |
|
|
destElement = $(this);
|
278 |
|
|
return false;
|
279 |
|
|
}
|
280 |
|
|
});
|
281 |
|
|
} else {
|
282 |
|
|
destElement = $collection.filter('[' + options.attribute + '="' + $(this).attr(options.attribute) + '"]');
|
283 |
|
|
}
|
284 |
|
|
if (destElement.length) {
|
285 |
|
|
|
286 |
|
|
if (!options.useScaling) {
|
287 |
|
|
animationQueue.push({
|
288 |
|
|
element : $(this), dest : destElement,
|
289 |
|
|
style : {
|
290 |
|
|
top : $(this).offset().top,
|
291 |
|
|
left : $(this).offset().left,
|
292 |
|
|
opacity : ""
|
293 |
|
|
},
|
294 |
|
|
animation : {
|
295 |
|
|
top : destElement.offset().top - correctionOffset.top,
|
296 |
|
|
left : destElement.offset().left - correctionOffset.left,
|
297 |
|
|
opacity : 1.0
|
298 |
|
|
}
|
299 |
|
|
});
|
300 |
|
|
} else {
|
301 |
|
|
animationQueue.push({
|
302 |
|
|
element : $(this), dest : destElement,
|
303 |
|
|
style : {
|
304 |
|
|
top : $(this).offset().top,
|
305 |
|
|
left : $(this).offset().left,
|
306 |
|
|
opacity : ""
|
307 |
|
|
},
|
308 |
|
|
animation : {
|
309 |
|
|
top : destElement.offset().top - correctionOffset.top,
|
310 |
|
|
left : destElement.offset().left - correctionOffset.left,
|
311 |
|
|
opacity : 1.0,
|
312 |
|
|
scale : '1.0'
|
313 |
|
|
}
|
314 |
|
|
});
|
315 |
|
|
}
|
316 |
|
|
} else {
|
317 |
|
|
|
318 |
|
|
if (!options.useScaling) {
|
319 |
|
|
animationQueue.push({
|
320 |
|
|
element : $(this),
|
321 |
|
|
style : {
|
322 |
|
|
top : $(this).offset().top,
|
323 |
|
|
left : $(this).offset().left,
|
324 |
|
|
opacity : ""
|
325 |
|
|
},
|
326 |
|
|
animation : {
|
327 |
|
|
opacity : '0.0'
|
328 |
|
|
}
|
329 |
|
|
});
|
330 |
|
|
} else {
|
331 |
|
|
animationQueue.push({
|
332 |
|
|
element : $(this),
|
333 |
|
|
animation : {
|
334 |
|
|
opacity : '0.0',
|
335 |
|
|
style : {
|
336 |
|
|
top : $(this).offset().top,
|
337 |
|
|
left : $(this).offset().left,
|
338 |
|
|
opacity : ""
|
339 |
|
|
},
|
340 |
|
|
scale : '0.0'
|
341 |
|
|
}
|
342 |
|
|
});
|
343 |
|
|
}
|
344 |
|
|
}
|
345 |
|
|
});
|
346 |
|
|
|
347 |
|
|
$collection.each(function(i) {
|
348 |
|
|
|
349 |
|
|
var sourceElement = [];
|
350 |
|
|
var destElement = [];
|
351 |
|
|
if (typeof (options.attribute) == 'function') {
|
352 |
|
|
val = options.attribute($(this));
|
353 |
|
|
$source.each(function() {
|
354 |
|
|
if (options.attribute(this) == val) {
|
355 |
|
|
sourceElement = $(this);
|
356 |
|
|
return false;
|
357 |
|
|
}
|
358 |
|
|
});
|
359 |
|
|
|
360 |
|
|
$collection.each(function() {
|
361 |
|
|
if (options.attribute(this) == val) {
|
362 |
|
|
destElement = $(this);
|
363 |
|
|
return false;
|
364 |
|
|
}
|
365 |
|
|
});
|
366 |
|
|
} else {
|
367 |
|
|
sourceElement = $source.filter('[' + options.attribute + '="' + $(this).attr(options.attribute) + '"]');
|
368 |
|
|
destElement = $collection.filter('[' + options.attribute + '="' + $(this).attr(options.attribute) + '"]');
|
369 |
|
|
}
|
370 |
|
|
|
371 |
|
|
var animationOptions;
|
372 |
|
|
if (sourceElement.length === 0 && destElement.length > 0) {
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
if (!options.useScaling) {
|
376 |
|
|
animationOptions = {opacity : '1.0'};
|
377 |
|
|
} else {
|
378 |
|
|
animationOptions = {opacity : '1.0', scale : '1.0'};
|
379 |
|
|
}
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
var d = destElement.clone();
|
383 |
|
|
var rawDestElement = d.get(0);
|
384 |
|
|
rawDestElement.style.position = 'absolute';
|
385 |
|
|
rawDestElement.style.margin = '0';
|
386 |
|
|
|
387 |
|
|
if (!options.adjustWidth) {
|
388 |
|
|
|
389 |
|
|
rawDestElement.style.width = width + 'px';
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
rawDestElement.style.top = destElement.offset().top - correctionOffset.top + 'px';
|
393 |
|
|
rawDestElement.style.left = destElement.offset().left - correctionOffset.left + 'px';
|
394 |
|
|
|
395 |
|
|
d.css('opacity', 0.0);
|
396 |
|
|
|
397 |
|
|
if (options.useScaling) {
|
398 |
|
|
d.css('transform', 'scale(0.0)');
|
399 |
|
|
}
|
400 |
|
|
d.appendTo($sourceParent);
|
401 |
|
|
|
402 |
|
|
if (options.maxWidth === 0 || destElement.offset().left < options.maxWidth) {
|
403 |
|
|
animationQueue.push({element : $(d), dest : destElement,animation : animationOptions});
|
404 |
|
|
}
|
405 |
|
|
}
|
406 |
|
|
});
|
407 |
|
|
|
408 |
|
|
$dest.remove();
|
409 |
|
|
if (!options.atomic) {
|
410 |
|
|
options.enhancement($sourceParent);
|
411 |
|
|
for (i = 0; i < animationQueue.length; i++) {
|
412 |
|
|
animationQueue[i].element.animate(animationQueue[i].animation, options.duration, options.easing, postCallback);
|
413 |
|
|
}
|
414 |
|
|
} else {
|
415 |
|
|
$toDelete = $sourceParent.find(options.selector);
|
416 |
|
|
$sourceParent.prepend($dest.find(options.selector));
|
417 |
|
|
for (i = 0; i < animationQueue.length; i++) {
|
418 |
|
|
if (animationQueue[i].dest && animationQueue[i].style) {
|
419 |
|
|
var destElement = animationQueue[i].dest;
|
420 |
|
|
var destOffset = destElement.offset();
|
421 |
|
|
|
422 |
|
|
destElement.css({
|
423 |
|
|
position : 'relative',
|
424 |
|
|
top : (animationQueue[i].style.top - destOffset.top),
|
425 |
|
|
left : (animationQueue[i].style.left - destOffset.left)
|
426 |
|
|
});
|
427 |
|
|
|
428 |
|
|
destElement.animate({top : "0", left : "0"},
|
429 |
|
|
options.duration,
|
430 |
|
|
options.easing,
|
431 |
|
|
postCallback);
|
432 |
|
|
} else {
|
433 |
|
|
animationQueue[i].element.animate(animationQueue[i].animation,
|
434 |
|
|
options.duration,
|
435 |
|
|
options.easing,
|
436 |
|
|
postCallback);
|
437 |
|
|
}
|
438 |
|
|
}
|
439 |
|
|
$toDelete.remove();
|
440 |
|
|
}
|
441 |
|
|
});
|
442 |
|
|
};
|
443 |
|
|
})(jQuery); |