Skip to content

Instantly share code, notes, and snippets.

@dennishall
Created July 9, 2012 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennishall/3078153 to your computer and use it in GitHub Desktop.
Save dennishall/3078153 to your computer and use it in GitHub Desktop.
Simple columnize javascript function
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment