Answer the question
In order to leave comments, you need to log in
How to align blocks in a row in jQuery?
There is a product output, let each product in the list have the .item class.
You need to align these blocks in height. But here's how to do it more elegantly, and in general, what algorithm for comparing block heights is best to use?
In this case, it is desirable to indicate the multiplicity of the comparison. For example, if the output goes for 5 products in a row. So you need to align the blocks not by the maximum height of the same block, but by the maximum value in each row.
Well, that's about it. Please tell me the correct solution
Answer the question
In order to leave comments, you need to log in
$.fn.setAllToMaxHeight = function() {
return this.height(Math.max.apply(this, $.map(this.children(), function(e) {
return $(e).height();
})));
};
$('.item').setAllToMaxHeight()
$(window).on("load resize", function () {
var maxHeight = 0;
$(".equal-group").find(".equal-item").height("auto").each(function () {
if ($(this).height() > maxHeight) {
maxHeight = $(this).height();
}
}).height(maxHeight);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question