2012-09-08 16 views

Odpowiedz

46

Można to zrobić przy użyciu jQuery:

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

Oto przykład: http://jsfiddle.net/DVnZ6/3/

+0

To jest fajne. Nie ma sposobu, aby to zrobić z css? – DaveR

+2

@DaveR Krótka odpowiedź: nie. Długa odpowiedź: możesz użyć stałej wysokości lub, jeśli znasz liczbę pól w dziale, użyj wysokości procentowej (100/N), ale to nie to samo. Weź pod uwagę, że bootstrap używa jQuery sam;) –

+1

@DaveR Istnieje inna opcja, ale działa tylko wtedy, gdy chcesz, aby wiele elementów div wyglądało jak wiersz tabeli: http://jsfiddle.net/DVnZ6/2/ –

-3

Jeśli ustawisz wysokość przęseł do 100% i ustawić wysokość wiersza do ustalonej wartości, wszystkie będą pasować do wysokości kontenera.

Oczywiście należy znać oczekiwaną wysokość kolumn, ale jest to rozwiązanie wolne js.

+1

Nie możesz ustawić 100% wysokości na elemencie bloku. –

1

Zdarzenie zmiany rozmiaru należy dodać do tego kodu, ponieważ wysokość zawartości w większości przypadków zależy od rozdzielczości i powoduje niepożądane problemy z projektowaniem, takie jak przepełniony tekst.

Oto bardziej kompletna wersja kodu, który obsługuje zmiany rozmiaru oraz

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

Sprawdź demo na jsFiddle http://jsfiddle.net/o4w7tjtz/

Wskazówka: spróbuj rozmiaru 3rd ramę pionową (zauważony równych wysokościach wokół 500pw szerokość?)

0

Mam łatwiejszy sposób, aby rozwiązać ten problem w elastyczny sposób:

//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); 
    } 

}); 
Powiązane problemy