【发布时间】:2020-08-12 06:56:27
【问题描述】:
问题:我想制作一个有 5 列的响应式布局。每列都有一个图像、一个标题和一个文本。我希望图像与所有其他图像异化,标题与标题异化,文本与文本。元素的高度应等于其所在行中较大元素的最大高度。
找到的解决方案:我通过迭代不同的元素获得了结果,我希望它具有相同的高度,检查最大高度并使用 javascript 将所有元素设置为这个高度。然后我添加了另一个脚本,每次调整窗口大小时都会再次执行此过程。这是我所拥有的code pen demo。
我在寻找什么:我在寻找实现这一结果的最佳实践方法。我想应该可以使用普通的 CSS。
谢谢大家!
$(document).ready(function() {
//Same height text
var max = 0;
$(".same-height").each(function(i) {
if ($(this).height() > max) {
max = $(this).height()
}
});
$(".same-height").height(max);
//same heigh image
var max2 = 0;
$(".same-height-img").each(function(i) {
if ($(this).height() > max2) {
max2 = $(this).height()
}
});
$(".same-height-img").height(max2);
//Execute the operation every time that the window resizes.
//Same height text
$( window ).resize(function() {
var max1 = 0;
//We need to set the height to auto first to get the initial value.
$(".same-height").css("height", "auto");
$(".same-height").each(function (i){
if($(this).height()>max1) {
max1 = $(this).height()
}
});
$(".same-height").height(max1);
//Same height image
var max2 = 0;
//We need to set the height to auto first to get the initial value.
$(".same-height-img").css("height", "auto");
$(".same-height-img").each(function (i){
if($(this).height()>max2) {
max2 = $(this).height()
}
});
$(".same-height-img").height(max2);
} );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" integrity="sha384-PDle/QlgIONtM1aqA2Qemk5gPOE7wFq8+Em+G/hmo5Iq0CCmYZLv3fVRDJ4MMwEA" crossorigin="anonymous">
<div class="container mt-5">
<div class="row">
<div class="col">
<div class="same-height-img"><img class="rounded mx-auto d-block " src="https://via.placeholder.com/150
C/O https://placeholder.com/ " alt=""></div>
<h1 class="text-center same-height">The title</h1>
<p class="text-justify">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Beatae, ad, eos nam fugiat labore quam quibusdam dignissimos. Largerrrrrrrr rrrrrrrrrrrrrrrr rrrrrrrrrrrrrrrrr rrrrrrrrrr
</p>
</div>
<div class="col">
<div class="same-height-img"><img class="rounded mx-auto d-block " src="https://via.placeholder.com/350
C/O https://placeholder.com/ " alt=""></div>
<h1 class="text-center same-height">The large large large title</h1>
<p class="text-justify">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Beatae, ad, eos nam fugiat labore quam quibusdam dignissimos.</p>
</div>
<div class="col">
<div class="same-height-img"><img class="rounded mx-auto d-block " src="https://via.placeholder.com/150
C/O https://placeholder.com/ " alt=""></div>
<h1 class="text-center same-height">The title</h1>
<p class="text-justify">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Beatae, ad, eos nam fugiat labore quam quibusdam dignissimos.</p>
</div>
<div class="col">
<div class="same-height-img"><img class="rounded mx-auto d-block " src="https://via.placeholder.com/150
C/O https://placeholder.com/ " alt=""></div>
<h1 class="text-center same-height">The title</h1>
<p class="text-justify">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Beatae, ad, eos nam fugiat labore quam quibusdam dignissimos.</p>
</div>
<div class="col">
<div class="same-height-img"><img class="rounded mx-auto d-block " src="https://via.placeholder.com/190
C/O https://placeholder.com/ " alt=""></div>
<h1 class="text-center same-height">The title</h1>
<p class="text-justify">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Beatae, ad, eos nam fugiat labore quam quibusdam dignissimos.</p>
</div>
</div>
</div>
【问题讨论】:
标签: javascript html jquery css bootstrap-4