A
A
Anton Seredny2016-01-23 01:24:47
JavaScript
Anton Seredny, 2016-01-23 01:24:47

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

2 answer(s)
A
Andrey, 2016-01-23
@smidl

$.fn.setAllToMaxHeight = function() {
  return this.height(Math.max.apply(this, $.map(this.children(), function(e) { 
    return $(e).height();
  })));
};

$('.item').setAllToMaxHeight()

this is how i did it in my working project
$(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);
        });

D
Dmitry Khristoev, 2016-01-23
@Haoss

https://github.com/mattbanks/jQuery.equalHeights

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question