jQuery 一组元素中具有最大高度的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6060992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
element with the max height from a set of elements
提问by lshettyl
I have a set of div
elements. In jQuery
, I would like to be able to find out the div
with the maximum height and also the height of that div
. For instance:
我有一组div
元素。在 中jQuery
,我希望能够找出div
最大高度以及那个高度div
。例如:
<div>
<div class="panel">
Line 1
Line 2
</div>
<div class="panel">
Line 1<br/>
Line 2<br/>
Line 3<br/>
Line 4<br/>
</div>
<div class="panel">
Line 1
</div>
<div class="panel">
Line 1
Line 2
</div>
</div>
By looking at the above, we know that the 2nd div
(with 4 Lines) has the maximum height of all. How do I find this out? Could someone please help?
通过查看上面的内容,我们知道第 2 条div
(有 4 条线)的高度最大。我怎么知道这个?有人可以帮忙吗?
So far I've tried:
到目前为止,我已经尝试过:
$("div.panel").height()
which returns the height of the 1st div
.
$("div.panel").height()
它返回 1st 的高度div
。
回答by Matt Ball
var maxHeight = Math.max.apply(null, $("div.panel").map(function ()
{
return $(this).height();
}).get());
If that's confusing to read, this might be clearer:
如果阅读起来令人困惑,这可能会更清楚:
var heights = $("div.panel").map(function ()
{
return $(this).height();
}).get();
maxHeight = Math.max.apply(null, heights);
回答by Vincent Ramdhanie
The html that you posted should use some <br>
to actually have divs with different heights. Like this:
您发布的 html 应该使用 some<br>
来实际拥有不同高度的 div。像这样:
<div>
<div class="panel">
Line 1<br>
Line 2
</div>
<div class="panel">
Line 1<br>
Line 2<br>
Line 3<br>
Line 4
</div>
<div class="panel">
Line 1
</div>
<div class="panel">
Line 1<br>
Line 2
</div>
</div>
Apart from that, if you want a reference to the div with the max height you can do this:
除此之外,如果您想引用具有最大高度的 div,您可以这样做:
var highest = null;
var hi = 0;
$(".panel").each(function(){
var h = $(this).height();
if(h > hi){
hi = h;
highest = $(this);
}
});
//highest now contains the div with the highest so lets highlight it
highest.css("background-color", "red");
回答by Diogo Gomes
If you want to reuse in multiple places:
如果要在多个地方重复使用:
var maxHeight = function(elems){
return Math.max.apply(null, elems.map(function ()
{
return $(this).height();
}).get());
}
Then you can use:
然后你可以使用:
maxHeight($("some selector"));
回答by maestro888
ul, li {
list-style: none;
margin: 0;
padding: 0;
}
ul {
display: flex;
flex-wrap: wrap;
}
ul li {
width: calc(100% / 3);
}
img {
width: 100%;
height: auto;
}
<ul>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="">
<br> Line 1
<br> Line 2
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="">
<br> Line 1
<br> Line 2
<br> Line 3
<br> Line 4
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="">
<br> Line 1
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="">
<br> Line 1
<br> Line 2
</li>
</ul>
回答by maestro888
function startListHeight($tag) {
function setHeight(s) {
var max = 0;
s.each(function() {
var h = $(this).height();
max = Math.max(max, h);
}).height('').height(max);
}
$(window).on('ready load resize', setHeight($tag));
}
jQuery(function($) {
$('#list-lines').each(function() {
startListHeight($('li', this));
});
});
#list-lines {
margin: 0;
padding: 0;
}
#list-lines li {
float: left;
display: table;
width: 33.3334%;
list-style-type: none;
}
#list-lines li img {
width: 100%;
height: auto;
}
#list-lines::after {
content: "";
display: block;
clear: both;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<ul id="list-lines">
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="" />
<br /> Line 1
<br /> Line 2
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="" />
<br /> Line 1
<br /> Line 2
<br /> Line 3
<br /> Line 4
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_sddu7-2560x1600.jpg" alt="" />
<br /> Line 1
</li>
<li>
<img src="http://img2.vetton.ru//upl/1000/346/138/vetton_ru_mixwall66-2560x1600.jpg" alt="" />
<br /> Line 1
<br /> Line 2
</li>
</ul>
回答by mhy
Try Find out divs height and setting div height(Similar to the one Matt posted but using a loop)
尝试找出 divs 高度并设置 div 高度(类似于 Matt 发布的那个但使用循环)
回答by davefrassoni
Easiest and clearest way I'd say is:
我要说的最简单、最清晰的方法是:
var maxHeight = 0, maxHeightElement = null;
$('.panel').each(function(){
if ($(this).height() > maxHeight) {
maxHeight = $(this).height();
maxHeightElement = $(this);
}
});
回答by Paul Kane
If you were interested in sorting entirely in standard JavaScript, or without using forEach():
如果您对完全使用标准 JavaScript 进行排序或不使用 forEach() 感兴趣:
var panels = document.querySelectorAll("div.panel");
// You'll need to slice the node_list before using .map()
var heights = Array.prototype.slice.call(panels).map(function (panel) {
// return an array to hold the item and its value
return [panel, panel.offsetHeight];
}),
// Returns a sorted array
var sortedHeights = heights.sort(function(a, b) { return a[1] > b[1]});