首页 > jq获取每4个一组的最大高度?

jq获取每4个一组的最大高度?

多个div每4个为一组,每个组将所有div的高度,都设置成该组最高的那个值


//获取所有div的高度,存入allDivHeight
var allDivHeight = []; 
$("div").each(function(index,element){
    allDivHeight.push($(this).height());   
});
//循环分组和设置最大高度
for (var j = 0 ; j<Math.ceil(allDivHeight.length/4); j++){
    var group = allDivHeight.slice(j*4,4*(j+1)); //每4个为一组
    var maxGroupHeight = Math.max.apply(null,group); //该组中最大的
    //设置该组中所有div的高度为最大高度(这里比较烂,没想到好方法)
    $("div").each(function(index,element){
        if(j*4<= index && index <= 4*(j+1)){
            $(this).height(maxGroupHeight);
        }
    });
} 

var a = Math.max(document.getElementById("a").offsetHeight, document.getElementById("b").offsetHeight, document.getElementById("c").offsetHeight);
document.getElementById("a").style.height = a + "px";
document.getElementById("b").style.height = a + "px";
document.getElementById("c").style.height = a + "px";


你想要的自适应高度吧!内层所有div块被最高的一个内容区域撑起,作为外层div块的高度。
如果你知道某个内层块会比其他高,那么这种情下,就可以直接css实现。

只需要将外层包裹层设置为:position:absolute;height:auto;
内层块使用:display:inline-block;height:100%;
内层块(非最高)设置为:position:absolute;
内层块(知道最高的那块区域)设置为:position:relative;

如果内层区域块不知道哪一块高,也就是随机的,那么就需要通过js计算一下。


var heighest    =    0;
$("div").each(function(){
    if($(this).height() > heighest){
        heighest = $(this).height();
    }
})
$("div").each(function(){
    $(this).css('height', heighest);
})
【热门文章】
【热门文章】