set equal height on multiple divs

前端 未结 5 1536
鱼传尺愫
鱼传尺愫 2020-12-21 15:49

I need to set equal height on a series of divs inside another div wrapper. The problem is that I dont want the same height on all of them. The page kind of have 3 columns an

相关标签:
5条回答
  • 2020-12-21 16:27

    Keep track of the widths and when they add up to the total width then resize the columns (keep track of which ones you've looked at OR how many might be easier) and then reset your currentTallest variable. So you'd be doing the resizing inside the loop every time you reached the end of the row.

    0 讨论(0)
  • 2020-12-21 16:30

    Perhaps you can loop through the divs, and check for $(this).offset().top If offset().top isn't equal to the last offset, you know you're on a different row.

    0 讨论(0)
  • 2020-12-21 16:31
    • EqualHeight.js - v1.0.1
    • https://github.com/JorenVanHee/EqualHeight.js

    this light weight plugin even support windows resize and responsive mode here is how to use:

    $(window).load(function() {
    $('.wrapper div').equalHeight({responsive: true});});
    
    0 讨论(0)
  • 2020-12-21 16:41

    I went with somethin like Peter suggested. I rewrote the plugin to:

    $.fn.cleverHeights = function(px) {
    $(this).each(function(){
        var currentTallest = 0;
        var width = 0;
        var row = new Array();
        $(this).children().each(function(i){
            if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
            width += parseInt($(this).outerWidth(), 10);
            row[++row.length] = $(this);
            if (width > 900) {
                $.each( row , function() {
                    $(this).css({'min-height': currentTallest});
                    // for ie6, set height since min-height isn't supported
                    if ($.browser.msie && $.browser.version == 6.0) { $(this).css({'height': currentTallest}); }
                });
                row.length = 0;
                width = 0;
                currentTallest = 0;
            }
        });
    });
    return this;
    };
    

    Thanks for the input

    0 讨论(0)
  • 2020-12-21 16:44

    Since you are using jQuery, are you using the EqualHeights plugin?

    http://www.cssnewbie.com/example/equal-heights/plugin.html

    Seems like you would have some logic to know how many divs are in a row before hitting the page width, then starting a new div row with a ID. Then call

    $('#custom-id').equalheights();
    

    as many times as you need to, and that should set them all to the same height per ID.

    You could build on that too by doing an each for divs in a row, and adding their width with .width().

    0 讨论(0)
提交回复
热议问题