Some improvements to Oskars code:
- By implementing
Elements
, the method can only be called on an Element collection (and the getSize
and setStyle
functions are always available).
- Using
map()
to get all heights.
- Returning the collection, so the method can be chained.
So the code becomes:
Elements.implement({
equalHeight: function(){
// Get height for all elements
var heights = this.map(function(el){
return el.getSize().y;
});
// Get maximum height
var maxHeight = Math.max.apply(Math, heights);
// Set maximum height to all elements
return this.setStyle('height', maxHeight);
}
});
And this could even be written as:
Elements.implement({
equalHeight: function(){
return this.setStyle('height', Math.max.apply(Math, this.map(function(el){
return el.getSize().y;
})));
}
});