4

我一直在使用 jQuery 的这个位(jQuery 等高响应 div 行)来获得相等的高度,它工作得很好,但是如果使用相同的模式,BS2 和 BS3,它不会扫描行以使高度相等,它扫描页面本身,然后同一页面上的所有模式(行>列--)获取页面上最高的高度。这不是期望的实现。

http://jsbin.com/aVavuLeW/14/

上面的 JSBin 有一个使用 BS3 列的示例(它的副本)。您不能在堆叠点的列上具有相对位置,因此已复制这些以用于此 Bin。如您所见,如果您在每一行(在此示例中为 .foo 和 .foo2)创建不同的类,然后列的行为,但如果您决定使用相同的模式,则页面上最高的高度将接管。

问题:如何解决使用相同模式时基于页面而不是行计算的问题?

谢谢!!!

4

1 回答 1

6

如果我正确理解您希望每一行具有相同的高度,基于最高的 div,而每一行具有相同的类名值,例如foo,那么下面的代码就是这样做的。这个想法是检查调整大小的元素的父级,如果父级发生变化,即在调整第二个 div 的大小时div.row,然后应用更改并重置最大高度的值。为了适用于指定的父级,引入了一个参数来指定父级选择器。 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();*/

  }); 
于 2013-11-28T19:05:38.950 回答