jQuery等于高度响应div行

时间:2012-10-23 11:06:16

标签: javascript jquery responsive-design equal-heights

我知道这个问题已被问了一百万次,但我正在寻找更具体的东西。

由于我的网站完全响应,我需要根据每行调整div的大小,而不是将所有div设置为一个高度。

我使用以下修改后的代码来设置容器中所有div的高度:

$.fn.eqHeights = function() {
    var el = $(this);
    if (el.length > 0 && !el.data('eqHeights')) {
        $(window).bind('resize.eqHeights', function() {
            el.eqHeights();
        });
        el.data('eqHeights', true);
    }
    return el.each(function() {
        var curTop = 0;
        var curHighest = 0;
        $(this).children().each(function(indx) {
            var el = $(this),
                elHeight = el.height('auto').outerHeight();

            var thisTop = el.position().top;
            if (curTop > 0 && curTop != thisTop) {
                curHighest = 0;
            }

            if (elHeight > curHighest) {
                curHighest = elHeight;
            }

            curTop = thisTop;

        }).height(curHighest);
    });
};

我有var thisTop = el.position().top;来确定哪些元素在同一行但不确定如何将同一行中的所有元素设置为最高值?

目前.height(curHighest);将所有元素设置为相同的高度,无论它们是否位于同一行。

感谢您的帮助。

3 个答案:

答案 0 :(得分:11)

使用以下代码段进行管理:

$.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);
};

我还添加了指定某个元素的功能,这样就可以获得高度变化。

<强>用法:

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

答案 1 :(得分:1)

我最近不得不做同样的事情,这就是我最终的结果:

$.fn.equalRowHeights = function() { 

    var count = this.length;

    // set element's height auto so it doesn't mess up when the window resizes
    this.height('auto');

    for (var i = 0; i < count; ) {

        // get offset of current element
        var x = this.eq(i).offset().top;

        // get elements in the same row
        var $rowEls = this.filter(function() {
            return $(this).offset().top === x;
        });

        // set the height elements in the same row
        $rowEls.height( 
            Math.max.apply(null, $rowEls.map(function() { 
                return $(this).height(); 
            }).get()); 
        );

        // set i to avoid unnecessary iterations
        i = $rowEls.filter(':last').index() + 1;    
    }

    var $this = this,
        timer = 0;

    // bind the resize event if it hasn't been added already 
    if (!$this.data('equalRowHeights')) {
        $(window).on('resize.equalRowHeights', function(e) {
            // add a buffer to prevent firing equalRowHeights() too much
            if (timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(function() {
                $this.equalRowHeights();
            }, 50);
        });
        $this.data('equalRowHeights', true);
    }   
};

Here's a fiddle

答案 2 :(得分:0)

我接受了dclawson的优秀答案并进行了一些编辑,因此它适用于其他情况,特别是嵌套元素,就像引导行中的commen一样。

$.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);
};

它使用相同的语法

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