Skip to content

Instantly share code, notes, and snippets.

@lutzissler
Created October 28, 2014 16:23
Show Gist options
  • Save lutzissler/18c58d8fd3b600adf97a to your computer and use it in GitHub Desktop.
Save lutzissler/18c58d8fd3b600adf97a to your computer and use it in GitHub Desktop.
Little jQuery plugin for spreading, or “partitioning“, elements across several parent elements. Called to create `n` partitions, the plugin wraps the selected elements at positions `c`, `n+c`, `2n+c`, `3n+c`, … into “wrapper” element for all `c` within `1`…`n`.
(function ($) {
$.fn.partition = function(options) {
options = $.extend({
count: false,
wrapper : '<div>'
}, options || {});
var elems = $(this),
len = pos = 0;
for (var i = 0; i < options.count; i++) {
var sel = elems.filter(":nth-child(" + (options.count - i) + "n)");
sel.wrapAll(options.wrapper);
elems = elems.not(sel);
}
return elems;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment