jQuery equal height responsive div rows

后端 未结 3 1074
猫巷女王i
猫巷女王i 2020-12-31 18:30

I know this question has been asked a million times but i\'m looking for something more specific.

As my site is fully responsive I need the divs to be resized based

3条回答
  •  被撕碎了的回忆
    2020-12-31 18:59

    I took dclawson's excellent answer and made some edits so it works in other situations, particularly with nested elements as is commen in bootstrap rows.

    $.fn.resEqHeight = function(options) {
        var defaults = {
            child: false
        };
        var options = $.extend(defaults, options);
    
        var $el = $(this);
        if ($el.length > 0) {
            if(!$el.data('resEqHeight')) {
                $(window).bind('resize.resEqHeight', function () {
                    $el.resEqHeight(options);
                });
                $el.data('resEqHeight', true);
            }
        } else {
            console.log("No items found for resEqHeight.");
        }
    
        if( options.child && options.child.length > 0 ){
            var elmtns = $(options.child, this);
        } else {
            var elmtns = $(this).children();
        }
    
        var prevTop = 0;
        var max_height = 0;
        var elements = [];
        elmtns
            .height('auto')
            .each(function() { $(this).data('resEqHeight.top', $(this).offset().top ); })
            .each(function(index) {
                var $el2 = $(this);
                var thisTop = $el2.data('resEqHeight.top');
    
                if (index > 0 && prevTop != thisTop) {
                    $(elements).height(max_height);
                    max_height = $el2.height();
                    elements = [];
                }
                max_height = Math.max(max_height, $el2.height());
    
                prevTop = thisTop;
                elements.push(this);
            });
    
        $(elements).height(max_height);
    };
    

    It works with the same syntax

    $('.equalheight').resEqHeight();
    $('.equalheight-frame').resEqHeight({child:'.frame'});
    

提交回复
热议问题