在多个div上设置相同的高度

发布于 2024-08-25 21:39:49 字数 1216 浏览 4 评论 0原文

我需要在另一个 div 包装器内的一系列 div 上设置相同的高度。问题是我不希望所有的高度都相同。页面类型有 3 列,浮动 div 可以是 1、2 或 3 列宽。 div 向左浮动,因此下面的示例将在包装器中提供三行 div。如何在同一行的div上设置相同的高度?在我的示例中,我希望 nr 1 和 2 具有相同的高度,而 3、4 和 5 具有相同的高度?我事先无法知道有多少个 div 或者它们有多宽或多高。 编辑: 例如,它们可以是 300、600 或 900 px 宽,页面宽度为 900px

<div id="wrapper">
 <div class="one-wide">nr1</div>
 <div class="two-wide">nr2</div>
 <div class="one-wide">nr3</div>
 <div class="one-wide">nr4</div>
 <div class="one-wide">nr5</div>
 <div class="three-wide">nr6</div>
</div>

我想我需要弄清楚 div 的添加宽度何时达到全页宽度并设置这些的高度相等。然后对下一个 div 执行相同的操作。但我无法理解它。目前我只是用它来设置包装器子项的高度:

$.fn.equalHeights = function(px) {
$(this).each(function(){
    var currentTallest = 0;
    $(this).children().each(function(i){
        if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
    });
    // for ie6, set height since min-height isn't supported
    if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
    $(this).children('div').css({'min-height': currentTallest}); 
});
return this;
};

I need to set equal height on a series of divs inside another div wrapper. The problem is that I dont want the same height on all of them. The page kind of have 3 columns and the floating divs can be 1, 2 or 3 columns wide. The divs float left, so the following example will give me three rows of divs in my wrapper. How can I set equal height on the divs that are in the same row? In my example I want nr 1 and 2 to have equal height and 3, 4 and 5 another equal height? I cant know beforehand how many divs there is or how wide or high they are. Edit: They can be for instance 300, 600 or 900 px wide and the page width is 900px

<div id="wrapper">
 <div class="one-wide">nr1</div>
 <div class="two-wide">nr2</div>
 <div class="one-wide">nr3</div>
 <div class="one-wide">nr4</div>
 <div class="one-wide">nr5</div>
 <div class="three-wide">nr6</div>
</div>

Im thinking I somehow need to figure out when the added width of the divs is at the full page width and set equal height on those. Then do the same on the next divs. But I cant wrap my head around it. Currently im just using this to set the height on the children of the wrapper:

$.fn.equalHeights = function(px) {
$(this).each(function(){
    var currentTallest = 0;
    $(this).children().each(function(i){
        if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
    });
    // for ie6, set height since min-height isn't supported
    if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
    $(this).children('div').css({'min-height': currentTallest}); 
});
return this;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷你仙 2024-09-01 21:39:49

跟踪宽度,当它们加起来达到总宽度时,调整列的大小(跟踪您查看过的列或可能更容易的列),然后重置 currentTallest 变量。因此,每次到达行尾时,您都会在循环内调整大小。

Keep track of the widths and when they add up to the total width then resize the columns (keep track of which ones you've looked at OR how many might be easier) and then reset your currentTallest variable. So you'd be doing the resizing inside the loop every time you reached the end of the row.

各自安好 2024-09-01 21:39:49

既然您使用的是 jQuery,那么您是否使用 EqualHeights 插件?

http://www.cssnewbie.com/example/equal-heights/plugin。 html

似乎你会有一些逻辑来知道在达到页面宽度之前一行中有多少个 div,然后使用 ID 开始一个新的 div 行。 调用

$('#custom-id').equalheights();

然后根据需要多次

,这应该将每个 ID 的高度设置为相同。您也可以在此基础上通过对一行中的 div 执行每个操作,并使用 .width() 添加它们的宽度。

Since you are using jQuery, are you using the EqualHeights plugin?

http://www.cssnewbie.com/example/equal-heights/plugin.html

Seems like you would have some logic to know how many divs are in a row before hitting the page width, then starting a new div row with a ID. Then call

$('#custom-id').equalheights();

as many times as you need to, and that should set them all to the same height per ID.

You could build on that too by doing an each for divs in a row, and adding their width with .width().

握住我的手 2024-09-01 21:39:49

也许你可以循环遍历 div,并检查 $(this).offset().top
如果 offset().top 不等于最后一个偏移量,您就知道您位于不同的行。

Perhaps you can loop through the divs, and check for $(this).offset().top
If offset().top isn't equal to the last offset, you know you're on a different row.

浮生面具三千个 2024-09-01 21:39:49

我按照彼得的建议去做了。我将插件重写为:

$.fn.cleverHeights = function(px) {
$(this).each(function(){
    var currentTallest = 0;
    var width = 0;
    var row = new Array();
    $(this).children().each(function(i){
        if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
        width += parseInt($(this).outerWidth(), 10);
        row[++row.length] = $(this);
        if (width > 900) {
            $.each( row , function() {
                $(this).css({'min-height': currentTallest});
                // for ie6, set height since min-height isn't supported
                if ($.browser.msie && $.browser.version == 6.0) { $(this).css({'height': currentTallest}); }
            });
            row.length = 0;
            width = 0;
            currentTallest = 0;
        }
    });
});
return this;
};

感谢您的输入

I went with somethin like Peter suggested. I rewrote the plugin to:

$.fn.cleverHeights = function(px) {
$(this).each(function(){
    var currentTallest = 0;
    var width = 0;
    var row = new Array();
    $(this).children().each(function(i){
        if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
        width += parseInt($(this).outerWidth(), 10);
        row[++row.length] = $(this);
        if (width > 900) {
            $.each( row , function() {
                $(this).css({'min-height': currentTallest});
                // for ie6, set height since min-height isn't supported
                if ($.browser.msie && $.browser.version == 6.0) { $(this).css({'height': currentTallest}); }
            });
            row.length = 0;
            width = 0;
            currentTallest = 0;
        }
    });
});
return this;
};

Thanks for the input

梦境 2024-09-01 21:39:49

这个轻量级插件甚至支持窗口调整大小和响应模式
以下是如何使用:

$(window).load(function() {
$('.wrapper div').equalHeight({responsive: true});});

this light weight plugin even support windows resize and responsive mode
here is how to use:

$(window).load(function() {
$('.wrapper div').equalHeight({responsive: true});});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文