简体   繁体   English

div高度相等

[英]Equal height for divs

I have a site . 我有一个网站 I want to make 3 vertical divs with equal height. 我想使3个垂直div具有相同的高度。 For this purposes I change the height of last block in each column/div. 为此,我更改了每个列/格中最后一个块的高度。

For example, the naming of 3 columns are: 例如,3列的命名为:

.leftCenter
.rightCenter
.right

Now I wrote a code which set the equal height for .leftCenter and .rightCenter : 现在,我编写了一个代码,为.leftCenter.rightCenter设置了相等的高度:

var left = $('.leftCenter').height();
var center = $('.rightCenter').height();

var news = $('#newItemsList').height();

if (center < left)
    $('.rightCenter').height(center + (left-center));
else if (center > left)
    $('#newItemsList').height(news + (center-left));

news is the latest subblock in left column (there are 3 images in it). news是左栏中的最新子块(其中有3张图像)。 So, if central div is bigger than left div, I change the height of news to make them equal. 因此,如果中央div大于左div,我将更改新闻的高度以使其相等。 This code works in Firefox , but doesn't work in Chrome. 此代码在Firefox中有效 ,但在Chrome中不起作用。 That's the first question. 那是第一个问题。 And the last is: how to make equal 3 divs (including right one). 最后是:如何使3个div相等(包括右一个)。

I needed to make elements equal in height and width so I made the following function that allows you to define a height, or width, or really whatever at it. 我需要使元素的高度和宽度相等,因此我做了以下函数,使您可以定义高度或宽度,或实际上定义的任何高度。 refType would be used if you sent a min-height and needed it to match the height of the tallest element. 如果您发送了最小高度并且需要它与最高元素的高度匹配,则将使用refType

elemsEqual = function (options) {
        var defaults = {
                'type' : 'height',
                'refType' : '',
                'elements' : [],
                'maxLen' : 450
            },
            settings = $.extend({}, defaults, options),
            max = 0;

        $(settings.elements.join(",")).each(function () {

            max = Math.max( max, parseInt( $(this).css(settings.type) ) );

            if(settings.refType.length) max = Math.max( max, parseInt( $(this).css(settings.refType) ) );

        });

        max = ((max < settings.maxLen) ? max : settings.maxLen);

        $(settings.elements.join(",")).css(settings.type, max + "px");
};

elemsEqual({elements : ['#selector1','#selector2', ... '#selectorN'], type : 'height'});

Well I have this so far: 好吧,到目前为止,我有:

//Get the height of the right column since it starts at a different Y position than the other two
var right=$('.right').outerHeight(1)-$('.left').children('header').outerHeight(1)-$('.left .innav').outerHeight(1);

//Get the max of the 3
var height_max=Math.max($('.leftCenter').outerHeight(1),$('.rightCenter').outerHeight(), right);

//Apply the max to all 3
$('.rightCenter').height(height_max-3); //-3 to accommodate for padding/margin
$('.right').height(height_max);
$('.leftCenter').height(height_max);

The only problem is that it does not make #newItemsList as tall as the parent, .leftCenter. 唯一的问题是,它不会使#newItemsList与父.leftCenter一样高。 It also assumes that the right div will be largest, so I don't know if it will still work if it isn't the biggest of the 3. 它还假定正确的div将是最大的,所以我不知道如果它不是3中最大的,它是否仍然可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM