2012-09-08 30 views

回答

46

您可以使用jQuery做到這一點:

boxes = $('.well'); 
maxHeight = Math.max.apply(
    Math, boxes.map(function() { 
    return $(this).height(); 
}).get()); 
boxes.height(maxHeight); 

下面是一個例子:http://jsfiddle.net/DVnZ6/3/

+0

很酷。沒有辦法做到這一點與CSS? – DaveR

+2

@DaveR簡短回答:沒有。長答案:你可以使用一個固定的高度,或者,如果你知道一個盒子的數量,使用百分比高度(100/N),但它不是一回事。記住採取引導用了jQuery本身;) –

+1

@DaveR還有另一種選擇,但前提是你要很多的div它工作看起來像一個錶行:http://jsfiddle.net/DVnZ6/2/ –

-3

如果設置了跨度爲100%的高度,並設置行的高度到一個固定值,它們都將與容器的高度相匹配。

當然,你必須知道該列的預期高度,但它是一個js免費的解決方案。

+1

不能設定爲100%的高度在塊元素上。 –

1

resize事件必須被添加到該代碼,因爲在大多數情況下,內容的高度上的不同分辨率不同而產生不必要的設計問題,如溢出的文本。

下面的代碼的更完整的版本支持上的jsfiddle 調整以及

jQuery(function() { 

    equalMaxWidth = 500; /* Customize viewport width - for resolutions below this number, the height equalizing will not work */ 
    boxes = jQuery('.well'); 

    boxes.removeAttr('style'); 
    if(jQuery(window).width() > equalMaxWidth){ 
     equalBoxHeights(boxes); 
    }; 

    window.onresize = function() { 
     boxes.removeAttr('style'); 
     console.log(jQuery(window).width()); 
     if(jQuery(window).width() > equalMaxWidth){ 
      equalBoxHeights(boxes); 
     }; 
    }; 
}); 

function equalBoxHeights(boxes) { 
    maxHeight = Math.max.apply(
      Math, boxes.map(function() { 
       return jQuery(this).height(); 
      }).get()); 
    boxes.height(maxHeight); 
} 

檢查演示http://jsfiddle.net/o4w7tjtz/

提示:嘗試調整第三縱框架(注意到相等的高度上繞500pw寬度?)

0

我確實有一個更簡單的方法來解決這個問題的響應方式:

//Make every Tile the same height(depending on the highest in row, no matter whats inside) 

$(document).ready(function() { 
    var boxes = $('.well'); 

    //Set the Height as in the example above on page Load 
    setHeight(); 

    // On viewportchange reset the height of each Element and again use the above example to set the heights 
    $(window).resize(function() { 
     boxes .css('height', 'auto'); 
     setHeight(); 
    }); 

    function setHeight() { 
     var maxHeight = Math.max.apply(
       Math, boxes.map(function() { 
        return $(this).height(); 
       }).get()); 
     boxes.height(maxHeight); 
    } 

}); 
相關問題