The following JavaScript code works only in Spidermonkey and Rhino, and it operates on E4X nodes--i.e., this is useful only for server-side JavaScript, but it might give someone a starting point for doing a jQuery version. (It's been very useful to me on the server side, but I haven't needed it on the client badly enough to actually build it.)
function columns(x,num) {
num || (num = 2);
x.normalize();
var cols, i, j, col, used, left, len, islist;
used = left = 0;
cols = <div class={'columns cols'+num}></div>;
if((left = x.length())==1) left = x.children().length();
else islist = true;
for(i=0; i<num; i++) {
len = Math.ceil(left/(num-i));
col = islist ? new XMLList : <{x.name()}></{x.name()}>;
if(!islist && x['@class'].toString()) col['@class'] = x['@class'];
for(j=used; j<len+used; j++) islist ? (col += x[j].copy()) : (col.appendChild(x.child(j).copy()));
used += len; left -= len;
cols.appendChild(<div class={'column'+(i==(num-1) ? 'collast' : '')}>{col}</div>);
}
return cols;
}
You call it like columns(listNode,2)
for two columns, and it turns:
<ul class="foo">
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
into:
<div class="columns cols2">
<div class="column">
<ul class="foo">
<li>a</li>
<li>b</li>
</ul>
</div>
<div class="column collast">
<ul class="foo">
<li>c</li>
</ul>
</div>
</div>
It's meant to be used with CSS like this:
div.columns { overflow:hidden; _zoom:1; }
div.columns div.column { float:left; }
div.cols2 div.column { width:47.2%; padding:0 5% 0 0; }
div.cols3 div.column { width:29.8%; padding:0 5% 0 0; }
div.cols4 div.column { width:21.1%; padding:0 5% 0 0; }
div.cols5 div.column { width:15.9%; padding:0 5% 0 0; }
div.columns div.collast { padding:0; }