如果第二个图像中的图像在代码中没有设置属性“height”,如何用mootools 1.12制作两个相同高度的文件?
<div id="box-1"></div>
<div id="box-2">
<img src="..." />
<img src="..." />
<img src="..." />
</div>
答案 0 :(得分:2)
Array.extend({
equalize: function(){
maxHeight = [];
this.each(function(el){
maxHeight.push(el.getSize().size.y);
});
this.setStyle('height', Math['max'].apply(Math, maxHeight));
}
});
$$('li').equalize(); // in your case $$('#box-1, #box-2')
onLoad
onDomReady
时显然已触发。
答案 1 :(得分:0)
对Oskars代码的一些改进:
Elements
,只能在Element集合上调用该方法(并且getSize
和setStyle
函数始终可用。)map()
获得所有高度。所以代码变成:
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);
}
});
这甚至可以写成:
Elements.implement({
equalHeight: function(){
return this.setStyle('height', Math.max.apply(Math, this.map(function(el){
return el.getSize().y;
})));
}
});