2013-03-04 68 views
0

下面的代碼應該找到最大列(.border)的高度,並調整.container div中找到的任何其他列的高度等於它。不幸的是,我一直無法獲得此代碼的工作,所以我希望有人比我更能幫助我。在窗口調整大小重新計算等高度列

還值得一提的是,每當窗口大小調整後,應重新計算列高度並重新調整列大小。

<script type="text/javascript"> 
    $(document).ready(function(){ 
     //Bind the window onresize event 
     $(window).bind('resize', resizeWindow); 

     //Call resizeWindow() function immediately to intiially set elements 
     resizeWindow(); 
    }); 

    function resizeWindow(){ 
     //Find all the container parent objects 
     $('.container').each(function(){ 
      //Initialize the height variable 
      var maxHeight = 0; 

      //Cache the jQuery object for faster DOM access and performance 
      var $borders = $(this).find('.border'); 

      //Find all the border child elements within this specific container 
      $borders.each(function(){ 
       //Get current element's height 
       var thisHeight = $(this).height(); 

       //Check if the current height is greater than the max height thus far 
       //If so, change max height to this height 
       if (thisHeight>maxHeight) maxHeight = thisHeight; 
      }); 

      //Now that we have the maximum height of the elements, 
      //set that height for all the .border child elements inside the parent element 
      $borders.height(maxHeight); 
     }); 
    } 
</script> 


<div class="container"> 
    <a href="#" class="border"> 
     <div class="column"> 
      <div class="content">asdf</div> 
     </div> 
    </a> 
    <a href="#" class="border"> 
     <div class="column"> 
      <div class="content">asdf</div> 
     </div> 
    </a> 
</div> 
+0

是否有任何理由,CSS'columns'不會做? – 2013-03-04 23:18:54

+0

@Kolink有。總結它不是一個選項。 – Rich 2013-03-04 23:21:32

回答

0

這不完全是解決JavaScript問題的方法。這是一個CSS解決方案,不需要任何JavaScript。使用這些樣式與您的標記,這兩個列將始終具有相同的高度:

div.container { 
    display: table; 
    width: 100px; 
} 

div.container > a { 
    display: table-cell; 
    border: 1px solid red; 
} 

演示

http://jsfiddle.net/wmbcr/

這將在大小調整工作太,如果沒有固定的寬度設置。

+0

你能說出原因嗎?也許我們可以找到一個不會破壞整個佈局的解決方案。 – insertusernamehere 2013-03-05 18:48:34

0

我想你應該提供一個高度,你DIV不是<a>

試試這個小提琴:

http://jsfiddle.net/ddFtX/1/

+0

這是爲什麼.....? – Rich 2013-03-04 23:48:02

+0

@rick你也可以做到這一點,但首先你需要設置它顯示:塊;看到這個http://stackoverflow.com/questions/4743254/setting-a-width-and-height-on-an-a-tag – Peeyush 2013-03-04 23:58:22

+0

我的錨被設置爲顯示:inline-block; – Rich 2013-03-05 00:03:13

相關問題