jQuery equal height responsive div rows

后端 未结 3 1090
猫巷女王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:58

    Managed to get this working with the following snippet:

    $.fn.eqHeights = function(options) {
    
        var defaults = {  
            child: false 
        };  
        var options = $.extend(defaults, options); 
    
        var el = $(this);
        if (el.length > 0 && !el.data('eqHeights')) {
            $(window).bind('resize.eqHeights', function() {
                el.eqHeights();
            });
            el.data('eqHeights', true);
        }
    
        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() {
    
            var thisTop = this.offsetTop;
    
            if (prevTop > 0 && prevTop != thisTop) {
                $(elements).height(max_height);
                max_height = $(this).height();
                elements = [];
            }
            max_height = Math.max(max_height, $(this).height());
    
            prevTop = this.offsetTop;
            elements.push(this);
        });
    
        $(elements).height(max_height);
    };
    

    I also added the ability to specify a certain element so that this gets the height change instead.

    Usage:

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

提交回复
热议问题