Created
April 24, 2012 07:19
-
-
Save jiggliemon/2477411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var columnize = function ( count , colClass ) { | |
colClass = colClass || 'span'+(12/count) | |
var columns = [],div | |
// builds the columns | |
while (count--) { | |
div = document.createElement('div') | |
div.setAttribute('class', colClass ) | |
columns.push(div) | |
} | |
return function (elements ){ | |
return columnize.parseElements(elements, columns) | |
} | |
} | |
columnize.parseElements = function ( elements , columns) { | |
var cols = columns.concat() | |
,frag = document.createDocumentFragment() | |
,col,div, i = 0, len = elements.length | |
// puts the elements into the columns | |
while(len--) { | |
if(!col || col == columns.length) col = 0 | |
cols[col++].appendChild(elements[i++].cloneNode(true)) | |
} | |
// puts the columns into a fragment | |
cols.forEach(function (el) { | |
frag.appendChild(el) | |
}) | |
return frag | |
} | |
columnize.two = columnize(2) | |
columnize.three = columnize(3) | |
columnize.four = columnize(4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pees = document.getElementsByTagName('p'); | |
var row = document.getElementById('row'); | |
var threeColumns = columnize(3); | |
row.appendChild(threeColumns(pees)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment