MooTools Equal Heights Plugin: Equalizer
Keeping equal heights between elements within the same container can be hugely important for the sake of a pretty page. Unfortunately sometimes keeping columns the same height can't be done with CSS -- you need a little help from your JavaScript friends. Well...now you're in luck.
The MooTools JavaScript
var Equalizer = new Class({
initialize: function(elements) {
this.elements = $$(elements);
},
equalize: function(hw) {
if(!hw) { hw = 'height'; }
var max = 0,
prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min-' : '') + hw; //ie6 ftl
offset = 'offset' + hw.capitalize();
this.elements.each(function(element,i) {
var calc = element[offset];
if(calc > max) { max = calc; }
},this);
this.elements.each(function(element,i) {
element.setStyle(prop,max - (element[offset] - element.getStyle(hw).toInt()));
});
return max;
}
});
The first step is cycle through each element to find which is the tallest (or widest -- this plugin accommodates for equal widths as well). Once we know the largest number we can set the min-property for the element to that number while subtracting border and padding. Simple!
The Sample Usage
var columnizer = new Equalizer('.sizeMe').equalize('height'); //call .equalize() as often as you want!
Obviously the class is super small. You create one instance, passing elements initially. From that point forward you can simply call the equalize method as many times as you would like. This is especially useful if the contents of a given column changes frequently.
Let me know if you see any room for improvement. I think this plugin will be quite useful for many developers.
Discussion
Be Heard!
Share your thoughts with fellow developers of all skill levels! I want to hear from you!
Great idea, encapsulating this in a class. I’ve done a similar thing before but just used a loop and had it messily embedded in my code.
Also, MooTools FTW!
Would be nice to extend Elements so you can do something like this:
$$(‘.someClass’).equalize()
or new Elements([new Element('div')]).equalize();
Elements.implement({
equalize: function(stop, prevent){
new Equalizer(this, stop, prevent).equalize();
return this;
}
});
line 15:
element.setStyle(prop,max – (element[offset] – element.getStyle(hw).toInt()));
…less code :) mootools ftw
Here’s the approach you suggested arian…
http://www.jsfiddle.net/sixtyseconds/U8EfF/
…and the Forge plugin: http://bit.ly/ds7tXB
Chris and David, David and Chris, you both saved me some time. Thanks! :D
@Florian, @Chris the Developer: Nice work!
A compliment from Mr Walsh! I feel so important now :D
@Chris the Developer says:
…and the Forge plugin: http://bit.ly/ds7tXB
http://www.mootools.net/forge/p/elements_equalize
The “Download” link is broken
@Chris the Developer: As you should!
I’ve added my plugin to the Forge: http://mootools.net/plugins/p/equalizer
Its not working properly on
Chrome 5.0.375.99 beta on Ubuntu
I have the same version ( minus beta ) on my window box, works fine.