Bootstrap 3 Equal Height Columns with jQuery need assistance with the js

后端 未结 1 1713
南笙
南笙 2020-12-25 09:23

I\'ve been using this bit (jQuery equal height responsive div rows) of jQuery for equal heights and it works very well, but with BS2 and BS3 if the same pattern is used, it

相关标签:
1条回答
  • 2020-12-25 09:43

    If i understood correctly that you want each row to have the same height, based on tallest div, while each row has the same class name values e.g. foo then the following code does that. The idea is to check the parent of the elements that are resized and if the parent has changed i.e. when resizing divs of the second div.row, then apply changes and reset the values of max height. In order to work for the specified parent a parameter has been introduced to specify the parent selector. http://jsbin.com/uBeQERiJ/1

    $.fn.eqHeights = function(options) {
    
        var defaults = {  
            child: false ,
          parentSelector:null
        };  
        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 = [];
        var parentEl;
        elmtns.height('auto').each(function() {
    
          if(options.parentSelector && parentEl !== $(this).parents(options.parentSelector).get(0)){
            $(elements).height(max_height);
            max_height = 0;
            prevTop = 0;
            elements=[];
            parentEl = $(this).parents(options.parentSelector).get(0);
          }
    
            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);
    };
    
    // run on load so it gets the size:
    // can't have the same pattern for some reason or it scans the page and makes all the same height. Each row should be separate but it doesn't work that way.
    $(window).load(function() {
    
    //$('[class*="eq-"]').eqHeights();
      $('.foo [class*="eq-"]').eqHeights({parentSelector:'.foo'});
    /*$('.foo2 [class*="eq-"]').eqHeights();*/
    
      }); 
    
    0 讨论(0)
提交回复
热议问题