2016-12-10 27 views
-1

div 2つを同じ高さにしたいと考えています。しかし、1つのdivは小さいものから1つは背が高いものです。私はいつも同じにすることができる方法を説明することができますか?divの高さを計算する

ここにいくつかのコードを持っている:

$(function() { 
    var a = $(.box1); 
    var b = $(.box2); 

    function calcHeight(a, b){ 
    if (a.height() > b.height()) { 
    b.css("height", a); 
    } else { 
    a.css("height", b); 
    } 
    } 

    calcHeight(a,b) 

    $(window).on('resize', function(){ 
    calcHeight(a,b) 
    }) 
}) 
<div class="flexcontainer"> ---rows are in flex-direction: row; 
<div class="row1"> --- boxes are one below other 
    <div class="box1"></div> 
    <div class="box2"></div> 
</div> 
<div class="row2"> 
    <div class="box3"></div> --- This box is full-height 
</div> 
<div class="row3"> --- boxes are one below other 
    <div class="box4"></div> 
    <div class="box5"></div> 
</div> 
</div> 

を私はbox5box2が常に同じ高さになりたいです。

ありがとうございます!

+0

あなたが同じ高さのdivのためのJSを必要としません。 CSSを使用することができます:http://stackoverflow.com/a/33815389/3597276 –

+1

'a'はjQueryオブジェクトです。' a.height() 'は高さの値です –

+0

b.css(" height " a);あなたはそれを自分自身に反対するためにCSSの高さの値を設定していますが、高さの値が必要です:b.css( "height"、a.height()+ 'px');とにかく... – sinisake

答えて

1

たぶん、あなたは次のことを試すことができます。

$(function() { 
 
    var a = $('.box1'); 
 
    var b = $('.box2'); 
 

 
    function calcHeight(a, b){ 
 
    var max_height = Math.max(a.height(), b.height())+'px'; 
 
    a.css('height', max_height); 
 
    b.css('height', max_height); 
 
    } 
 

 
    calcHeight(a,b) 
 

 
    $(window).on('resize', function(){ 
 
    calcHeight(a,b) 
 
    }) 
 
})
<div class="flexcontainer"> ---rows are in flex-direction: row; 
 
<div class="row1"> --- boxes are one bolow other 
 
    <div class="box1"></div> 
 
    <div class="box2"></div> 
 
</div> 
 
<div class="row2"> 
 
    <div class="box3"></div> --- This box is full-height 
 
</div> 
 
<div class="row3"> --- boxes are one bolow other 
 
    <div class="box4"></div> 
 
    <div class="box5"></div> 
 
</div> 
 
</div>

+0

@TklnTky 私はいくつか考えています。どのように私は特定のクラスのすべてのボックスを通過することができます、最高のボックスを探して、最高の高さを取って、すべてのボックスに置く? すべてのサイズ変更と再読み込みにも使用します。 ありがとうございます! – Bane

+0

Hmm ..すべてのボックスに同じクラスを追加し、最大の高さを計算します(すべてをループし、最大のものを格納します)。そして、この高さをすべてのボックスに適用します。 $( '。box')。それぞれ(function(){if(max_height <$(this))> {max_height = $(this)} .height();} $( '。box')。 CSS( '高さ':MAX_HEIGHT + 'PX'); –

+0

@TklnTky私はこのコードと分の高さがあります:310pxを、CSSで $(関数(){ 関数setEqualHeight(クラス名){ VAR最大= 0; $(クラス名).each(関数(指数){ VAR電流= $(この).height(); 場合(現在> MAX){ 最大=現在; }}); $ (className).css( 'height'、max + 'px'); } var boxUnit = '.box1'; $(ウィンドウ).on( 'load'、setEqualHeight(boxUnit));$(ウィンドウ).on( 'サイズ変更'、setEqualHeight(boxUnit)); }) そして、それは1920pxの高さでリロード時に私を設定しました:310px;この高さはすべての低いビークポイントにあります。 – Bane

関連する問題