简体   繁体   English

每行高度相等

[英]Equal heights per row

I would like to set equal heights on all items. 我想在所有物品上设置相同的高度。 I am currently using this example: 我目前正在使用此示例:

   var heights = $(".well").map(function() {
        return $(this).height();
    }).get(),

    maxHeight = Math.max.apply(null, heights);

    $(".well").height(maxHeight);

Based on this question Same height column bootstrap 3 row responsive 基于此问题相同高度的列引导程序3行响应

I would like to apply this on a per row basis, as some heights are being set too high for rows that do not need so be set. 我想逐行应用此设置,因为对于不需要设置的行,某些高度设置得太高。

well its possible with css also if you use display:flex on parent div then all the inner div will come with same height in same row but if its need of jquery then use this 如果在父div上使用display:flex ,它也可能与css一起使用,那么所有内部div都将在同一行中具有相同的高度,但是如果需要jQuery,则使用此

CODE

var maxHeight;
$(".well").each(function(){
  if($(this).height > maxHeight){
    maxHeight = $(this).height;// will assign the highest height to this
  }
})
$('.well').css('height',maxHeight );

All the div will be of same height according to the highest size div 根据最大尺寸div,所有div的高度均相同

For examples sake Lets assume you are out putting your html via a php loop (if not then you can simply loop though the elems assigning a class) same principle applies. 例如,让我们假设您是通过php循环放置html(如果没有,则可以简单地通过elems分配类),同样的原理也适用。

PHP example outputting html PHP示例输出HTML

    <?php foreach($array as $k => $v): ?>
        <?php if($k % (4) == 0) $class++; ?>

        <div class="row-num-<?php echo $class ?>">
            // div content variable height
        </div>
    <?php endforeach; ?>

jQuery Setting Equal Height per row using your existing code jQuery使用现有代码设置每行相等的高度

        var totalFrames = $('div[class*=row-num-]').length;
        for ( var i = 1, l = totalFrames; i < l; i++ ) {
            var heights = $(".row-num-"+i).map(function() {
                    return $(this).height();
                }).get(),

                maxHeight = Math.max.apply(null, heights);

            $(".row-num-"+i).height(maxHeight);
        }

This assumes your row is 4 elems wide. 假设您的行宽为4个元素。 You can set this or dynamically calc it depending on your use. 您可以根据需要设置或动态计算它。 In the jQuery we loop through all elems with a class like row-num- then we target each one setting the height. 在jQuery中,我们使用row-num- num这样的类遍历所有元素,然后针对每个row-num-设置高度。 These will have the same class every four elems. 每四个元素将具有相同的类。 It continues until the current count of this class name has been reached. 它将继续直到达到该类名称的当前计数。

DEMO https://jsfiddle.net/DTcHh/18026/ 演示 https://jsfiddle.net/DTcHh/18026/

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

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