Получить externalHeight класса и принять наибольшее значение
Я использую .outerHeight
установить высоту другого div, используя класс в качестве селектора.
var $example = $('.example');
var $height = $example.outerHeight();
var $styles = { 'height': $height }
$('.wrapper_sub').css($styles);
Я хочу использовать это на нескольких "слайдах" моего сайта:
<div class="wrapper">
<div class="example">Some Content</div>
<div class="wrapper_sub">Other Content</div>
</div>
<div class="wrapper">
<div class="example">Some Content</div>
<div class="wrapper_sub">Other Content</div>
</div>
<div class="wrapper">
<div class="example">Some Content</div>
<div class="wrapper_sub">Other Content</div>
</div>
Как я могу получить .outerHeight
каждого .example
, принять только самое высокое значение и добавить это ко всем .wrapper_sub
дивы?
2 ответа
Решение
Смотрите комментарии в строке:
var maxHeight = 0; // Initialize to zero
var $example = $('.example'); // Cache to improve performance
$example.each(function() { // Loop over all the elements having class example
// Get the max height of elements and save in maxHeight variable
maxHeight = parseFloat($(this).outerHeight()) > maxHeight ? parseFloat($(this).outerHeight()) : maxHeight;
});
$('.wrapper_sub').height(maxHeight); // Set max height to all example elements
Перебрать .example
элементы и получить максимальное значение. Затем примените это значение к этим элементам:
//Set an empty array
var arr = [];
//Loop through the elements
$('.example').each(function() {
//Push each value into the array
arr.push(parseFloat($(this).outerHeight()));
});
//Get the max value with sort function
var maxH = arr.sort(function(a,b) { return b-a })[0];
//Apply the max value to the '.example' elements
$('.example').css({'height': maxH + 'px'});