Skip to content

Instantly share code, notes, and snippets.

@patw0929
Last active July 26, 2016 03:08
Show Gist options
  • Save patw0929/d7240c15ce04bb1cdb1fd1ad75e068e9 to your computer and use it in GitHub Desktop.
Save patw0929/d7240c15ce04bb1cdb1fd1ad75e068e9 to your computer and use it in GitHub Desktop.
Make masonry layout has almost same height in every columns
var imageHeights = [5, 10, 100, 2, 200, 30, 90, 20, 44, 60, 12, 2, 10, 4, 5, 92, 10];
var baseArr = imageHeights.slice(0, 5);
var remainArr = imageHeights.slice(5);
var getMinIndex = function(arr) {
var min = arr[0];
var index = 0;
for (var i = 0; i < 5; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
return index;
};
var arrangeMasonrySameHeight = function () {
for (var i = 0, max = remainArr.length; i < max; i++) {
baseArr[getMinIndex(baseArr)] += remainArr[i];
}
return baseArr;
};
console.log(arrangeMasonrySameHeight());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment