Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jstnbr/d36fc51c9a0d15eba09cf27713f03bf3 to your computer and use it in GitHub Desktop.
Save jstnbr/d36fc51c9a0d15eba09cf27713f03bf3 to your computer and use it in GitHub Desktop.
/**
* easyListSplitter 1.0.2
* Copyright (c) 2010 Andrea Cima Serniotti (http://www.madeincima.eu)
* Dual licensed under MIT and GPL
*/
/**
* Usage:
*
* $('ul').easyListSplitter({
* colNumber: 3,
* direction: 'horizontal'
* });
*/
var j = 1;
(function(jQuery) {
jQuery.fn.easyListSplitter = function(options) {
var defaults = {
colNumber: 2,
direction: 'vertical'
};
this.each(function() {
var obj = $(this);
var settings = jQuery.extend(defaults, options);
var totalListElements = $(this).children('li').size();
var baseColItems = Math.ceil(totalListElements / settings.colNumber);
var listClass = $(this).attr('class');
// Create List Elements given colNumber
for (i = 1; i <= settings.colNumber; i++) {
if (i == 1) {
$(this).addClass('listCol1').wrap('<div class="listContainer'+j+'"></div>');
} else if ($(this).is('ul')) { // Check whether the list is ordered or unordered
$(this).parents('.listContainer'+j).append('<ul class="listCol'+i+'"></ul>');
} else {
$(this).parents('.listContainer'+j).append('<ol class="listCol'+i+'"></ol>');
}
$('.listContainer'+j+' > ul, .listContainer'+j+' > ol').addClass(listClass);
}
var listItem = 0;
var k = 1;
var l = 0;
// Append List Elements to the respective listCol - Vertical
if (settings.direction == 'vertical') {
$(this).children('li').each(function () {
listItem = listItem+1;
if (listItem > baseColItems*(settings.colNumber-1)) {
$(this).parents('.listContainer'+j).find('.listCol'+settings.colNumber).append(this);
}
else {
if (listItem<=(baseColItems*k)) {
$(this).parents('.listContainer'+j).find('.listCol'+k).append(this);
}
else {
$(this).parents('.listContainer'+j).find('.listCol'+(k+1)).append(this);
k = k+1;
}
}
});
$y('.listContainer'+j).find('ol, ul').each(function () {
if($(this).children().size() == 0) {
$(this).remove();
}
});
}
// Append List Elements to the respective listCol - Horizontal
else {
$(this).children('li').each(function(){
l = l+1;
if (l <= settings.colNumber){
$(this).parents('.listContainer'+j).find('.listCol'+l).append(this);
}
else {
l = 1;
$(this).parents('.listContainer'+j).find('.listCol'+l).append(this);
}
});
}
// Set class last on the last ul or ol
$('.listContainer'+j).find('ol:last,ul:last').addClass('last');
j = j+1;
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment