【发布时间】: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