简体   繁体   English

jQuery Equal Height Divs

[英]jQuery Equal Height Divs

If i have the following markup; 如果我有以下标记;

<div id="container">
  <div id="box">
    <div id='sameHeight'>One<br>two<br>three</div>
    <div id='sameHeight'>four</div>
    <div id='sameHeight'>five</div>        
  <div>
  <div id="box">
    <div id='sameHeight'>four</div>
    <div id='sameHeight'>six</div>
    <div id='sameHeight'>seven<br>eight</div>
  <div>
</div>

How can I ensure that all divs marked as "sameHeight" are the same height as their counterparts in the other div? 如何确保标记为“sameHeight”的所有div与其他div中的div相同?

I had a look at the equalHeights plugin but that assumes all divs side by side are in the same parent. 我看了一下equalHeights插件,但是假设并排的所有div都在同一个父节点中。 I need one that can either traverse parents or allow me to specify parents. 我需要一个可以遍历父母或允许我指定父母的人。

Is there such a thing or do I need to write it? 有这样的事情还是我需要写它?

EDIT 编辑

I seem to have caused some confusion in my explanation so I hope this clears things up a little. 我似乎在我的解释中引起了一些混乱,所以我希望这会让事情变得清晰起来。

Looking at the new markup, the container is a simple box. 查看新标记,容器是一个简单的盒子。

The "box" divs sit side by side. “盒子”divs并排坐着。

Each sameheight div then sits one under the other within its parent. 然后每个sameheight div在其父级中位于另一个之下。

The thing i'm trying to solve is to have each of the sameHeights that match to it's opposite side the same height. 我试图解决的问题是让每个相同的高度匹配它的相对高度。

it should look like a grid i guess w/out using a grid. 它应该看起来像一个网格我想用w / out使用网格。

I hope this helps. 我希望这有帮助。

EDIT 2 编辑2

This is so far what I have come up with but is there a better way? 这是迄今为止我想出的,但有更好的方法吗?

function SetHeights() {
    var numLines = $('#container>div:eq(0) .sameHeight').length;

    for (var t = 0; t < numLines; t++) {
        var leftHeight = $('#container>div:eq(0) .sameHeight:eq(' + t + ')').outerHeight();
        var rightHeight = $('#container>div:eq(1) .sameHeight:eq(' + t + ')').outerHeight();

        if (leftHeight > rightHeight) {
            $('#container>div:eq(1) .sameHeight:eq(' + t + ')').css({ height: leftHeight });
        }
        else {
            $('#container>div:eq(0) .sameHeight:eq(' + t + ')').css({ height: rightHeight });
        }
    }
}

Firstly, you are probably aware only one element should have any one id attribute. 首先,您可能知道只有一个元素应该具有任何一个id属性。 So I have changed the selectors as if they were classes to classes below. 所以我已经改变了选择器,好像它们是下面的类的类。 Even though you may not give a care about W3C standards, browsers or JavaScript API, etc may rely on this behaviour and not work now or in the future. 即使你可能不关心W3C标准,浏览器或JavaScript API等也可能依赖于这种行为而现在或将来都无法工作。

   $(document).ready(function () {    
        $('div.parentDiv > div').each(function() {

        var $sameHeightChildren = $(this).find('.sameHeight');
        var maxHeight = 0;

        $sameHeightChildren.each(function() {
            maxHeight = Math.max(maxHeight, $(this).outerHeight());
        });

        $sameHeightChildren.css({ height: maxHeight + 'px' });


    });
});

Note : If you want them all to be the same height despite their parent div, this is what you want. 注意 :如果你希望它们都是相同的高度,尽管它们是父div,这就是你想要的。

$(document).ready(function () {    
        var $sameHeightDivs = $('.sameHeight');
        var maxHeight = 0;
        $sameHeightDivs.each(function() {

            maxHeight = Math.max(maxHeight, $(this).outerHeight());

        });

        $sameHeightDivs.css({ height: maxHeight + 'px' });
});

This will set them to all be the height of the tallest, per parent div element. 这将它们设置为每个父div元素的最高高度。

Also, this answer may show you some other options. 此外, 这个答案可能会显示一些其他选项。

Note too that if the content inside changes again (perhaps via JavaScript), you will need to call this again, or put it in a setInterval() which I do not recommend. 另请注意,如果内部内容再次更改(可能通过JavaScript),则需要再次调用它,或者将其放在我建议的setInterval()中。

<div id='parentDiv'>
  <div>
      <div id='sameHeight'>One<br>two<br>three</div>
      <div id='sameHeight'>four</div>
      <div id='sameHeight'>five</div>        
  <div>
  <div>
      <div id='sameHeight'>four</div>
      <div id='sameHeight'>six</div>
      <div id='sameHeight'>seven<br>eight</div>
  <div>
</div>

and in JavaScript using jQuery write : 并在JavaScript中使用jQuery编写:

$(document).ready(function () {    
    $("div.parentDiv div div.sameHeight").css({ height: "100px" });
});

Why not just use a table? 为什么不用一张桌子?

Imho, doing layout in javascript like this is far more evil than doing the layout via tables. Imho,像这样在javascript中进行布局比通过表格进行布局更加邪恶。 What happens if the user resizes their browser? 如果用户调整浏览器大小会怎样? You have to capture the resize event, but then you get a certain laggyness when resizing that makes your website look unpolished. 您必须捕获调整大小事件,但是在调整大小时会出现一定的滞后性,这会使您的网站看起来没有修饰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM