过去,我曾使用此脚本将同一类的所有 div 设置为最高 div 的高度。
equalheight = function(container){
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array(),
$el,
topPosition = 0;
$(container).each(function() {
$el = $(this);
$($el).height('auto')
topPostion = $el.position().top;
if (currentRowStart != topPostion) {
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].attr("style", "height: " + currentTallest + "px !important;");
}
rowDivs.length = 0; // empty the array
currentRowStart = topPostion;
currentTallest = $el.height();
rowDivs.push($el);
} else {
rowDivs.push($el);
currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
}
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].attr("style", "height: " + currentTallest + "px !important;");
}
});
}
$(document).ready(function() {
equalheight('.divs_to_match');
});
但是,这一次,我尝试选择 SHORTEST div,然后将其他 div 的最大高度设置为匹配,以便将 div 裁剪到相同的高度。有人可以帮我修改它以实现这一目标吗?
编辑 - 这是一个 jsfiddle 显示它当前的工作方式 - https://jsfiddle.net/mortalwombat7/h61oc67x/11/