如何使用jQuery将多个DIV保持相同的高度?

Zac*_*son 5 html css jquery layout

我有一些带有不同文本内容的DIV标签.

HTML:

<div id="boxes">
    <div id="boxone">
        <p>...</p>
    </div>
    <div id="boxtwo">
        <p>...</p>
    </div>
    <div id="boxthree">
        <p>...</p>
    </div>
    <div id="boxfour">
        <p>...</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

它们采用2×2布局,宽度适中:

CSS:

div#boxes {
    width: 100%;
}

div#boxes div {
    width: 49.9%;
    float: left;
}
Run Code Online (Sandbox Code Playgroud)

我希望他们都有相同的高度.

所以,我循环遍历它们并找到最高的高度.然后我再次循环并将它们全部设置到那个高度.

jQuery的:

$(function() {
    var maxHeight = 0;
    $('div#boxes div').each(function(){
        if (maxHeight < $(this).height()) {maxHeight = $(this).height()}
    });
    $('div#boxes div').each(function(){
        $(this).height(maxHeight);
    });
});
Run Code Online (Sandbox Code Playgroud)

如果div的高度不需要再次更改,这很有效.

但是,如果我调整浏览器窗口大小,则会失败:

  1. 如果我(a)使浏览器更宽,那么(b)我的DIV变得更宽,然后(c)他们的文本内容包裹更少次,然后(d)我的DIV太高了.

  2. 如果我(b)使浏览器更窄,那么(b)我的DIV变得更窄,然后(c)他们的文本内容包含更多,然后(d)我的DIV太短.

我如何(1)自动将DIV大小调整到正常的内容高度,还(2)保持多个DIV的高度相同?

Jar*_*rco 5

对于像我一样的其他人,通过搜索谷歌寻求解决方案来到这里:接受的答案的第一部分只有在第一个div最高时才有效.我对它做了一些修改,现在它似乎适用于所有情况.

var highest = 0;
function sortNumber(a,b)    {
    return a - b;
}

function maxHeight() {
    var heights = new Array();
    $('#wrapper2>div').each(function(){
        $(this).css('height', 'auto');
        heights.push($(this).height());
        heights = heights.sort(sortNumber).reverse();
    });        
        highest = heights[0]; 
    $('#wrapper2>div').each(function(){
        $(this).css('height', highest);
    });

}

$(document).ready(function() {
    maxHeight();
})

$(window).resize(maxHeight);
Run Code Online (Sandbox Code Playgroud)


Rww*_*wwL 4

更新...在尝试并找到另一种显然可行的方法后完全重写了这个答案:

function sortNumber(a,b)    {
    return a - b;
}

function maxHeight() {
    var heights = new Array();
    $('div#boxes div').each(function(){
        $(this).css('height', 'auto');
        heights.push($(this).height());
        heights = heights.sort(sortNumber).reverse();
        $(this).css('height', heights[0]);
    });        
}

$(document).ready(function() {
    maxHeight();
})

$(window).resize(maxHeight);
Run Code Online (Sandbox Code Playgroud)

我注意到的一件事是,IE 对于 50% 宽的浮动 div 确实存在舍入问题……如果我将其更改为 49%,渲染效果会好得多。

这个 jQuery 工作...

// global variables

doAdjust = true;
previousWidth = 0;

// raise doAdjust flag every time the window width changes

$(window).resize(function() {
    var currentWidth = $(window).width();
    if (previousWidth != currentWidth) {
        doAdjust = true;
    }
    previousWidth = currentWidth;
})

// every half second

$(function() {
    setInterval('maybeAdjust()', 500);
});

// check the doAdjust flag

function maybeAdjust() {
    if (doAdjust) {
        adjustBoxHeights();
        doAdjust = false;
    }
}

// loop through the DIVs and find the height of the tallest one
// then loop again and set them all to that height

function adjustBoxHeights() {
    var maxHeight = 0;
    $('div#boxes div').each(function(){
        $(this).height('auto');
        if (maxHeight < $(this).height()) {maxHeight = $(this).height()}
    });
    $('div#boxes div').each(function(){
        $(this).height(maxHeight);
    });
}
Run Code Online (Sandbox Code Playgroud)