Skip to content

Instantly share code, notes, and snippets.

@clemgrim
Created August 3, 2015 14:32
Show Gist options
  • Save clemgrim/33b2bfd1a05ac6608882 to your computer and use it in GitHub Desktop.
Save clemgrim/33b2bfd1a05ac6608882 to your computer and use it in GitHub Desktop.
// layout for only 2 columns
function layout (container, selector) {
var ct = container.jquery ? container : $(container);
var themes = ct.find(selector);
var colLeft = 0;
var colRight = 0;
// resize and position `.theme` elements
themes.each (function (idx) {
var elm = $(this);
var height = elm.outerHeight();
var deltaLeft = colLeft + height;
var deltaRight = colRight + height;
var isLeft;
// Determine on which column put the current element
if (deltaRight === deltaLeft) {
isLeft = idx%2 === 0;
} else {
isLeft = deltaRight > deltaLeft;
}
elm.css({
position: 'absolute',
left: isLeft ? 0 : elm.outerWidth(),
top: isLeft ? colLeft : colRight,
});
// increment columns height
isLeft ? colLeft += height : colRight += height;
});
// apply height to the parent
ct.height(Math.max(colLeft, colRight));
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment