I am able to do the list using float:left;
like this
But
Below is a columnizer, takes as argument the number of columns.
Call: $(elem).columnize(3)
http://jsfiddle.net/Bdsj9/28/
Tested in IE6 from wine in Ubuntu 10.04: works (looks better if you increase the width in the stylesheet I borrowed from @micha -- thanks, btw)
(function($) {
$.fn.decolumnize = function() {
this.children().map(function(index, el) {
var oldPos = null;
var posClass = null;
if($(el).attr("class") && (posClass = $(el).attr("class").match(/orig\-readorder\-[0-9]+/))) {
oldPos = parseInt(posClass[0].replace("orig-readorder-", ""));
$(el).removeClass(posClass[0]);
}
return {
elm: el,
pos: oldPos ? oldPos : index
}
}).sort(function(a,b) {
return a.pos > b.pos ? 1 : -1;
}).map(function(index, ob) {
return ob.elm;
}).appendTo(this);
return this.children().css("float", "left").css("clear", "none");
};
$.fn.columnize = function(numcols) {
var numItems = this.children().length;
var divisor = Math.ceil(numItems / numcols);
var indexOfFinal = null;
this.decolumnize();
var resorted = this.children().map(function(index, el) {
return {
position: (index % divisor) + (index / numItems),
elm: el,
isFinal: index == numItems - 1,
origPos: index
};
}).sort(function(a, b) {
return a.position > b.position ? 1 : -1;
});
return resorted.map(function(index, ob) {
if (indexOfFinal) {
/** TODO: fix redundancy **/
if ((index - indexOfFinal - 1) % (numcols - 1) == 0) {
if ($.browser.msie && resorted[index - 1]) $(resorted[index - 1].elm).css("float", "none");
$(ob.elm).css("clear", "left");
}
} else {
/** TODO: fix redundancy **/
if (index % numcols == 0) {
if ($.browser.msie && resorted[index - 1]) $(resorted[index - 1].elm).css("float", "none");
$(ob.elm).css("clear", "left");
}
}
if (ob.isFinal) indexOfFinal = index;
$(ob.elm).addClass("orig-readorder-" + ob.origPos);
return ob.elm;
}).appendTo(this);
};
})(jQuery);
What it does is first calculate the sortorder, because with just styling this will not work. To calculate the sortorder you need to know the number of columns up front. This you can use as a divisor to introduce a 'clear: left'.
Also, using the number of list items and the number of columns you need to estimate a number of rows. This can be used as a divisor to sort the items based on the remainer between their index and the number of rows.
To granulate the sortation, the original index is also taken into account...
After the last orginal item has been placed, the number of columns needs to be reduced by 1. That's why I store the isFinal-boolean. I'm sure this could be done more elegantly with some smart computation, though.
Then introduce the 'clear: left's at the right place and store the original position in a class so you can resort in a later stage (for instance when inserting or removing a list item dynamically)
Best!