【问题标题】:Change height of a class with JQuery for each element "foreach"使用 JQuery 为每个元素“foreach”更改类的高度
【发布时间】:2019-09-06 13:43:11
【问题描述】:

我有一个从 processwire 检索信息的 foreach。 在每一行中,左侧有一个图像 (class= Img1),右侧有一个 div (class= Test2) 内的描述。 我想测量图像和包含描述的 div 的高度,选择最大的并将其设置为包含图像的 div 的高度(class= Test1)。 实际代码的问题在于,在第一次迭代时,最大高度被存储并应用于所有行,而不仅仅是第一行。

我知道我应该使用 each() 迭代函数,我试图了解它是如何工作的,但我没有成功。 有人可以帮帮我吗?

HTML

    <?php $Ser = $pages->get("name=services");?>
<section id="<?=$Ser->title;?>" class="mea-service-section section-padding-shorter">
  <?php $i = 0;
      foreach($Ser->ServicesRepeater as $Ser):
      $image = $Ser->ServicesRepeaterImage2;

      $colFirst = "";
      // if ($i%3 == 0) $colFirst = 'col_first'; //
      $i++; ?>
      <div class="container">

    <div class="row mt-10" style="display: flex;">


    <!-- Services Widget Section -->
    <div class='outer-row' id="<?=$Ser->ServicesRepeaterHeadline;?>">
      <div class="col-md-5 mea-title-section wow animated slideInLeft" data-wow-delay=".2s">
      <div class="media-left-center vertical-align Test1">
        <img src="<?=$image->url;?>" alt="<?=$image->description;?>" class="Img1 img-responsive center-block rcorners3">
      </div>
    </div>
    <div class="col-md-7 <?=$colFirst;?> single-service-widget wow animated fadeInUp" data-wow-delay=".3s">
      <div class="Test2">
        <div class="media-body">
          <h2 class="subtitle"><?=$Ser->ServicesRepeaterHeadline ?></h2>
          <p><?=$Ser->ServicesRepeaterDescription ?></p>
        </div>
      </div>
    </div>
    </div>

  </div>
</div>
<?php endforeach; ?>
</section>

jQuery

var divImgHeight = function() {
  var divHeight = $('.Test2').height();
  var imgHeight = $('.Img1').height();
  if (imgHeight < divHeight) {
    $('.Test1').css('height', divHeight+'px');
  } else {
    $('.Test1').css('height', imgHeight+'px');
  }
}

$(document).ready(function() {

  function resizeLinks(){
    divImgHeight();
  }
  $(window).on("resize", resizeLinks);
  resizeLinks();

    });

实际正在建设的页面:https://brightnode.io/en/services/

【问题讨论】:

  • “存储和应用最大高度” - 不,它不是“最大”。它是类Test2(或Img1)的第一个匹配元素的高度 -> .height(): "获取匹配集合中第一个元素的当前计算高度元素”
  • 由于您的示例 HTML 中只有一个“行”,因此很难确定 .Test1.Test2.Img1 相互应用的位置(尤其是组合缩进不一致)
  • 我已经在 HTML 代码中添加了整个部分。你现在可以看到foreach了。对不起!

标签: jquery foreach height each


【解决方案1】:

假设外部 div 是一个“行”,给外部 div 一个类以便它可以被引用:

<div class='outer-row' id="<?=$Ser->ServicesRepeaterHeadline;?>">
  <div class="col-md-5 mea-title-section wow animated slideInLeft" data-wow-delay=".2s">
    <div class="media-left-center vertical-align Test1">
      <img src="<?=$image->url;?>" alt="<?=$image->description;?>" class="Img1 img-responsive center-block rcorners3">
    </div>
  </div>
  <div class="col-md-7 <?=$colFirst;?> single-service-widget wow animated fadeInUp" data-wow-delay=".3s">
    <div class="Test2">
      <div class="media-body">
        <h2 class="subtitle"><?=$Ser->ServicesRepeaterHeadline ?></h2>
        <p><?=$Ser->ServicesRepeaterDescription ?></p>
      </div>
    </div>
  </div>
</div>

然后遍历.outer-row 并将您的代码应用到每一行,使用this 引用该行,例如:

$(".outer-row").each(function() { 

  // at this point "this" is each of the .outer-row divs in turn

  var divHeight = $('.Test2', this).height();
  // or
  var imgHeight = $(this).find('.Img1').height();

给予:

var divImgHeight = function() {
  $(".outer-row").each(function() { 
    var row = $(this);  
    var divHeight = $('.Test2', row).height();
    var imgHeight = $('.Img1', row).height();
    if (imgHeight < divHeight) {
      $('.Test1', row).css('height', divHeight+'px');
    } else {
      $('.Test1', row).css('height', imgHeight+'px');
    }
  });
}

$(document).ready(function() {
  function resizeLinks() {
    divImgHeight();
  }
  $(window).on("resize", resizeLinks);
  resizeLinks();
});

编辑

要在所有项目中应用最大高度,您首先需要遍历项目以获得最大高度,然后遍历每一行以应用。您可以应用一些快捷方式,但概念是相同的。

var divImgHeight = function() {
  // get all the heights from .Test2/.Img1
  var heights = $(".outer-row .Test2,.outer-row .Img1").map(function() { return $(this).height(); }).toArray();
  // get the largest height
  var maxheight = Math.max.apply(Math, heights);

  // apply to all .Test1s
  $(".outer-row .Test1").css('height', maxHeight+'px');
}

【讨论】:

  • 现在可以了,谢谢!如果我想查看所有行中最大的 div 并将该高度应用于所有 orws 怎么办?
猜你喜欢
  • 1970-01-01
  • 2021-01-13
  • 2012-02-08
  • 2018-12-05
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
相关资源
最近更新 更多