public
Created

Simple columnize javascript function

  • Download Gist
columnize.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
function columnize(ul){
var offsetTop = YAHOO.util.Dom.getRegion(ul).top;
var listItems = ul.getElementsByTagName('li');
for(var i=0,l=listItems.length;i<l;i++){
var region = YAHOO.util.Dom.getRegion(listItems[i]);
if(region.bottom - offsetTop > 200){
var clone = ul.cloneNode(true);
// remove all list items that follow this one from the current list. (will be covered by the clone)
for(var j=l-1; j>=i; j--){
ul.removeChild( ul.lastChild );
}
// remove all list items that precede this one from the clone. (are covered by the original list)
for(var j=i; j>0; j--){
clone.removeChild(clone.firstChild);
}
ul.parentNode.appendChild(clone);
// recurse - check whether the clone also needs to be columnized (if it's too tall)
columnize(clone);
return;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.