I have several divs on a page in columns of 2, so every 2 divs needs to be the same height. As far as I can tell the plugins available for to force equal height do all elements the same height, I just need each pair to be the same height.
my markup looks like this
etc
I have the following jquery which works, but I am sure it can be done easier
{
var rowOneHeight = Math.max($('div.large:eq(0)').height(),$('div.large:eq(1)').height());
$('div.large:eq(0)').css('min-height', rowOneHeight);
$('div.large:eq(1)').css('min-height', rowOneHeight);
var rowTwoHeight = Math.max($('div.large:eq(2)').height(),$('div.large:eq(3)').height());
$('div.large:eq(2)').css('min-height', rowTwoHeight);
$('div.large:eq(3)').css('min-height', rowTwoHeight);
var rowThreeHeight = Math.max($('div.large:eq(4)').height(),$('div.large:eq(5)').height());
$('div.large:eq(4)').css('min-height', rowThreeHeight);
$('div.large:eq(5)').css('min-height', rowThreeHeight);
}
it would also need the same to be done with a group of three divs. Thanks
You could do a loop with .slice()
to loop through in sets of 2, like this:
var elems = $("div.large");
for(var i = 0; i < elems.length; i+=2) {
var divs = elems.slice(i, i+2),
height = Math.max(divs.eq(0).height(), divs.eq(1).height());
divs.css('min-height', height);
}
Or, for a more general approach, since you want to do it with 3 as well, here's a plugin form:
$.fn.setMinHeight = function(setCount) {
for(var i = 0; i < this.length; i+=setCount) {
var curSet = this.slice(i, i+setCount),
height = 0;
curSet.each(function() { height = Math.max(height, $(this).height()); })
.css('min-height', height);
}
return this;
};
Then you can call it like this:
$("div.large").setMinHeight(2); //or 3, etc
You can test it out here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With