Skip to content

Instantly share code, notes, and snippets.

@yentsun
Created February 1, 2012 08:05
Show Gist options
  • Save yentsun/1715894 to your computer and use it in GitHub Desktop.
Save yentsun/1715894 to your computer and use it in GitHub Desktop.
An example of even spreading of li elements (with child li elements) within a set number of columns using jQuery.
$(function(){
var columns_no = 4;
$('.popup').each(function(){
var total_height = $(this).find('li:not(.brands_column li)').length;
var column = $(this).find('.column1');
var average_height = Math.ceil(total_height / columns_no);
column.data('height', 0);
var total_categories = $(this).find('li.depth1').length;
$(this).find('li.depth1').each(function(){
if (total_categories == columns_no) {
do_append($(this), column)
column = cycle_column(column);
} else if (column.data('height') >= average_height) {
column = cycle_column(column);
do_append($(this), column);
} else {
do_append($(this), column);
}
});
});
});
function do_append(category, column) {
var cat_height = category.find('li').length;
var to_add = 1;
if (cat_height == 1)
to_add = 2;
category.appendTo(column);
column.data('height',column.data('height') + cat_height + to_add);
}
function cycle_column(column) {
column = column.next();
column.data('height', 0)
return column;
}
@yentsun
Copy link
Author

yentsun commented Feb 1, 2012

Visual representation: http://goo.gl/aC0wd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment