Letlabo Nature Reserve
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

jquery.quicksand.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Quicksand 1.2.2
  3. Reorder and filter items with a nice shuffling animation.
  4. Copyright (c) 2010 Jacek Galanciak (razorjack.net) and agilope.com
  5. Big thanks for Piotr Petrus (riddle.pl) for deep code review and wonderful docs & demos.
  6. Dual licensed under the MIT and GPL version 2 licenses.
  7. http://github.com/jquery/jquery/blob/master/MIT-LICENSE.txt
  8. http://github.com/jquery/jquery/blob/master/GPL-LICENSE.txt
  9. Project site: http://razorjack.net/quicksand
  10. Github site: http://github.com/razorjack/quicksand
  11. */
  12. (function ($) {
  13. $.fn.quicksand = function (collection, customOptions) {
  14. var options = {
  15. duration: 750,
  16. easing: 'swing',
  17. attribute: 'data-id', // attribute to recognize same items within source and dest
  18. adjustHeight: 'auto', // 'dynamic' animates height during shuffling (slow), 'auto' adjusts it before or after the animation, false leaves height constant
  19. useScaling: true, // disable it if you're not using scaling effect or want to improve performance
  20. enhancement: function(c) {}, // Visual enhacement (eg. font replacement) function for cloned elements
  21. selector: '> *',
  22. dx: 0,
  23. dy: 0
  24. };
  25. $.extend(options, customOptions);
  26. if ($.browser.msie || (typeof($.fn.scale) == 'undefined')) {
  27. // Got IE and want scaling effect? Kiss my ass.
  28. options.useScaling = false;
  29. }
  30. var callbackFunction;
  31. if (typeof(arguments[1]) == 'function') {
  32. var callbackFunction = arguments[1];
  33. } else if (typeof(arguments[2] == 'function')) {
  34. var callbackFunction = arguments[2];
  35. }
  36. return this.each(function (i) {
  37. var val;
  38. var animationQueue = []; // used to store all the animation params before starting the animation; solves initial animation slowdowns
  39. var $collection = $(collection).clone(); // destination (target) collection
  40. var $sourceParent = $(this); // source, the visible container of source collection
  41. var sourceHeight = $(this).css('height'); // used to keep height and document flow during the animation
  42. var destHeight;
  43. var adjustHeightOnCallback = false;
  44. var offset = $($sourceParent).offset(); // offset of visible container, used in animation calculations
  45. var offsets = []; // coordinates of every source collection item
  46. var $source = $(this).find(options.selector); // source collection items
  47. // Replace the collection and quit if IE6
  48. if ($.browser.msie && $.browser.version.substr(0,1)<7) {
  49. $sourceParent.html('').append($collection);
  50. return;
  51. }
  52. // Gets called when any animation is finished
  53. var postCallbackPerformed = 0; // prevents the function from being called more than one time
  54. var postCallback = function () {
  55. if (!postCallbackPerformed) {
  56. postCallbackPerformed = 1;
  57. // hack:
  58. // used to be: $sourceParent.html($dest.html()); // put target HTML into visible source container
  59. // but new webkit builds cause flickering when replacing the collections
  60. $toDelete = $sourceParent.find('> *');
  61. $sourceParent.prepend($dest.find('> *'));
  62. $toDelete.remove();
  63. if (adjustHeightOnCallback) {
  64. $sourceParent.css('height', destHeight);
  65. }
  66. options.enhancement($sourceParent); // Perform custom visual enhancements on a newly replaced collection
  67. if (typeof callbackFunction == 'function') {
  68. callbackFunction.call(this);
  69. }
  70. }
  71. };
  72. // Position: relative situations
  73. var $correctionParent = $sourceParent.offsetParent();
  74. var correctionOffset = $correctionParent.offset();
  75. if ($correctionParent.css('position') == 'relative') {
  76. if ($correctionParent.get(0).nodeName.toLowerCase() == 'body') {
  77. } else {
  78. correctionOffset.top += (parseFloat($correctionParent.css('border-top-width')) || 0);
  79. correctionOffset.left +=( parseFloat($correctionParent.css('border-left-width')) || 0);
  80. }
  81. } else {
  82. correctionOffset.top -= (parseFloat($correctionParent.css('border-top-width')) || 0);
  83. correctionOffset.left -= (parseFloat($correctionParent.css('border-left-width')) || 0);
  84. correctionOffset.top -= (parseFloat($correctionParent.css('margin-top')) || 0);
  85. correctionOffset.left -= (parseFloat($correctionParent.css('margin-left')) || 0);
  86. }
  87. // perform custom corrections from options (use when Quicksand fails to detect proper correction)
  88. if (isNaN(correctionOffset.left)) {
  89. correctionOffset.left = 0;
  90. }
  91. if (isNaN(correctionOffset.top)) {
  92. correctionOffset.top = 0;
  93. }
  94. correctionOffset.left -= options.dx;
  95. correctionOffset.top -= options.dy;
  96. // keeps nodes after source container, holding their position
  97. $sourceParent.css('height', $(this).height());
  98. // get positions of source collections
  99. $source.each(function (i) {
  100. offsets[i] = $(this).offset();
  101. });
  102. // stops previous animations on source container
  103. $(this).stop();
  104. var dx = 0; var dy = 0;
  105. $source.each(function (i) {
  106. $(this).stop(); // stop animation of collection items
  107. var rawObj = $(this).get(0);
  108. if (rawObj.style.position == 'absolute') {
  109. dx = -options.dx;
  110. dy = -options.dy;
  111. } else {
  112. dx = options.dx;
  113. dy = options.dy;
  114. }
  115. rawObj.style.position = 'absolute';
  116. rawObj.style.margin = '0';
  117. rawObj.style.top = (offsets[i].top - parseFloat(rawObj.style.marginTop) - correctionOffset.top + dy) + 'px';
  118. rawObj.style.left = (offsets[i].left - parseFloat(rawObj.style.marginLeft) - correctionOffset.left + dx) + 'px';
  119. });
  120. // create temporary container with destination collection
  121. var $dest = $($sourceParent).clone();
  122. var rawDest = $dest.get(0);
  123. rawDest.innerHTML = '';
  124. rawDest.setAttribute('id', '');
  125. rawDest.style.height = 'auto';
  126. rawDest.style.width = $sourceParent.width() + 'px';
  127. $dest.append($collection);
  128. // insert node into HTML
  129. // Note that the node is under visible source container in the exactly same position
  130. // The browser render all the items without showing them (opacity: 0.0)
  131. // No offset calculations are needed, the browser just extracts position from underlayered destination items
  132. // and sets animation to destination positions.
  133. $dest.insertBefore($sourceParent);
  134. $dest.css('opacity', 0.0);
  135. rawDest.style.zIndex = -1;
  136. rawDest.style.margin = '0';
  137. rawDest.style.position = 'absolute';
  138. rawDest.style.top = offset.top - correctionOffset.top + 'px';
  139. rawDest.style.left = offset.left - correctionOffset.left + 'px';
  140. if (options.adjustHeight === 'dynamic') {
  141. // If destination container has different height than source container
  142. // the height can be animated, adjusting it to destination height
  143. $sourceParent.animate({height: $dest.height()}, options.duration, options.easing);
  144. } else if (options.adjustHeight === 'auto') {
  145. destHeight = $dest.height();
  146. if (parseFloat(sourceHeight) < parseFloat(destHeight)) {
  147. // Adjust the height now so that the items don't move out of the container
  148. $sourceParent.css('height', destHeight);
  149. } else {
  150. // Adjust later, on callback
  151. adjustHeightOnCallback = true;
  152. }
  153. }
  154. // Now it's time to do shuffling animation
  155. // First of all, we need to identify same elements within source and destination collections
  156. $source.each(function (i) {
  157. var destElement = [];
  158. if (typeof(options.attribute) == 'function') {
  159. val = options.attribute($(this));
  160. $collection.each(function() {
  161. if (options.attribute(this) == val) {
  162. destElement = $(this);
  163. return false;
  164. }
  165. });
  166. } else {
  167. destElement = $collection.filter('[' + options.attribute + '=' + $(this).attr(options.attribute) + ']');
  168. }
  169. if (destElement.length) {
  170. // The item is both in source and destination collections
  171. // It it's under different position, let's move it
  172. if (!options.useScaling) {
  173. animationQueue.push(
  174. {
  175. element: $(this),
  176. animation:
  177. {top: destElement.offset().top - correctionOffset.top,
  178. left: destElement.offset().left - correctionOffset.left,
  179. opacity: 1.0
  180. }
  181. });
  182. } else {
  183. animationQueue.push({
  184. element: $(this),
  185. animation: {top: destElement.offset().top - correctionOffset.top,
  186. left: destElement.offset().left - correctionOffset.left,
  187. opacity: 1.0,
  188. scale: '1.0'
  189. }
  190. });
  191. }
  192. } else {
  193. // The item from source collection is not present in destination collections
  194. // Let's remove it
  195. if (!options.useScaling) {
  196. animationQueue.push({element: $(this),
  197. animation: {opacity: '0.0'}});
  198. } else {
  199. animationQueue.push({element: $(this), animation: {opacity: '0.0',
  200. scale: '0.0'}});
  201. }
  202. }
  203. });
  204. $collection.each(function (i) {
  205. // Grab all items from target collection not present in visible source collection
  206. var sourceElement = [];
  207. var destElement = [];
  208. if (typeof(options.attribute) == 'function') {
  209. val = options.attribute($(this));
  210. $source.each(function() {
  211. if (options.attribute(this) == val) {
  212. sourceElement = $(this);
  213. return false;
  214. }
  215. });
  216. $collection.each(function() {
  217. if (options.attribute(this) == val) {
  218. destElement = $(this);
  219. return false;
  220. }
  221. });
  222. } else {
  223. sourceElement = $source.filter('[' + options.attribute + '=' + $(this).attr(options.attribute) + ']');
  224. destElement = $collection.filter('[' + options.attribute + '=' + $(this).attr(options.attribute) + ']');
  225. }
  226. var animationOptions;
  227. if (sourceElement.length === 0) {
  228. // No such element in source collection...
  229. if (!options.useScaling) {
  230. animationOptions = {
  231. opacity: '1.0'
  232. };
  233. } else {
  234. animationOptions = {
  235. opacity: '1.0',
  236. scale: '1.0'
  237. };
  238. }
  239. // Let's create it
  240. d = destElement.clone();
  241. var rawDestElement = d.get(0);
  242. rawDestElement.style.position = 'absolute';
  243. rawDestElement.style.margin = '0';
  244. rawDestElement.style.top = destElement.offset().top - correctionOffset.top + 'px';
  245. rawDestElement.style.left = destElement.offset().left - correctionOffset.left + 'px';
  246. d.css('opacity', 0.0); // IE
  247. if (options.useScaling) {
  248. d.css('transform', 'scale(0.0)');
  249. }
  250. d.appendTo($sourceParent);
  251. animationQueue.push({element: $(d),
  252. animation: animationOptions});
  253. }
  254. });
  255. $dest.remove();
  256. options.enhancement($sourceParent); // Perform custom visual enhancements during the animation
  257. for (i = 0; i < animationQueue.length; i++) {
  258. animationQueue[i].element.animate(animationQueue[i].animation, options.duration, options.easing, postCallback);
  259. }
  260. });
  261. };
  262. })(jQuery);