Skip to content

Instantly share code, notes, and snippets.

@rnmp
Created September 23, 2012 02:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnmp/3768546 to your computer and use it in GitHub Desktop.
Save rnmp/3768546 to your computer and use it in GitHub Desktop.
DATA-COLUMNS

For more information about this project, follow this link.

// DATA-COLUMNS by Rolando Murillo (@rnmp)
// and Giorgio Leveroni (@ppold)
var grids = document.querySelectorAll('[data-columns]');
[].forEach.call(grids, function(grid) {
var columns = grid.dataset.columns,
columnClass = '_col',
itemClass = '_item',
elements = [];
for (var i = columns - 1; i >= 0; --i) {
elements.push(grid.querySelectorAll('.'+itemClass+':nth-child('+columns+'n-'+i+')'));
}
elements.forEach(function(columnElements) {
var column = document.createElement('div');
column.classList.add(columnClass);
Array.prototype.forEach.call(columnElements, function(element) {
column.appendChild(element);
});
grid.appendChild(column);
});
});
@rnmp
Copy link
Author

rnmp commented Oct 29, 2012

[].forEach is faster than Array.prototype.forEach according to: http://jsperf.com/foreach-vs-array-prototype-foreach

@ppold
Copy link

ppold commented Feb 21, 2013

Actually storing the forEach function in a variable is the fastest (using the same link you mentioned).

P.S. I'll implement the responsive support and the addition/deletion of content next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment