2013-10-19 34 views
0

背景: 我使用CSS創建一個Web應用程序的體驗,在網頁上的一切總是可見的,只是在某些panes是滾動的。我現在需要將其擴展到表格。我希望表頭始終保持可見(有時是頁腳),但表格主體只能滾動。粘表頭與100%的高度「窗格」

我到目前爲止所做的工作是對theadtfoot應用「窗格」類,然後使tbody自動填充高度,然後使其滾動。所有這些工作除了thead,tbody和tfoot不再是100%寬度。這種方法似乎讓我離我想要的最近。

小提琴: 我有一個的jsfiddle這讓我接近,這也顯示出我使用創建的固定和滾動panes代碼。 http://jsfiddle.net/va4HF/1/

要求:理想我想保持我的pane CSS,因爲它是,但與之相適應需要一個表時。我也想只保留這個CSS。

以下代碼是我的概念證明代碼,並且還包含面板CSS。

HTML:

<div class="column"> 
    <div class="pane top">Top</div> 
    <div class="pane middle"> 
     <table> 
      <thead class="pane table-head"> 
       <tr> 
        <th>Header</th> 
       </tr> 
      </thead> 
      <tbody class="pane table-body fill"> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
      </tbody> 
      <tfoot class="pane table-foot"> 
       <tr> 
        <th>Footer</th> 
       </tr> 
      </tfoot> 
     </table> 
    </div> 
    <div class="pane bottom">Bottom</div> 
</div> 

CSS:

body, html { 
    height:100%; 
    margin:0; 
    padding:0; 
} 
div { 
    box-sizing:border-box; 
    box-sizing:content-box\9; /* Fix for IE9 sizing */ 
    -moz-box-sizing:border-box; /* Fix for firefox sizing */ 
} 
.column { 
    position:absolute; 
    width:100%; 
    min-height:100%; 
    height:auto; 
    overflow:hidden; 
} 
.pane { 
    position:absolute; 
    width:100%; 
    overflow:hidden; 
} 
.fill { 
    height:auto; 
    overflow:auto; 
} 
.top { 
    background:pink; 
    height: 20%; 
    top:0; 
} 
.middle { 
    background:red; 
    top:20%; 
    bottom:50px; 
} 
.table-head { 
    height: 40px; 
    top:0; 
} 
.table-body { 
    top:40px; 
    bottom:40px; 
} 
.table-foot { 
    height: 40px; 
    bottom:0; 
} 
.bottom { 
    background:orange; 
    height: 50px; 
    bottom:0; 
} 
table thead tr, table tfoot tr {background:rgba(0,0,0,0.5);} 
table tbody tr {background:rgba(0,0,0,0.3);} 
table td, table th {padding:10px; color:#fff;} 

/* Fix for Safari scrollbars (also styles Chrome scrollbars) */ 
    .fill::-webkit-scrollbar {-webkit-appearance: none;width: 10px;} 
    .fill::-webkit-scrollbar-track {background-color: rgba(0,0,0, .3);border-radius: 0px;} 
    .fill::-webkit-scrollbar-thumb {border-radius: 0px; background-color: rgba(255,255,255, .4);} 

研究: 別人已經在Table that Fills 100% Parent Height with Fixed Header/Footer問到這個類似的情況,但一直沒有得到很好的響應,並結束隨着Javascript,但我想嘗試找到一個CSS解決方案。或者至少是一個簡單的JavaScript解決方案,他用那什麼(避免疊加三個表)

回答

1

大量的研究後找出所有的我爲此選擇了選擇使用separ吃了標題,正文和頁腳的表格,並用div隔開每個表格。然後我將該div設置爲「窗格」。只要標題表和主體表中的列被設置爲相同的寬度,它們就會排隊。

有一些我不得不做的清理,其中一個導致需要保持垂直滾動條總是顯示,然後在頁眉和頁腳表中添加一個頁邊距,這樣如果主體開始滾動或不滾動,標題將始終排隊。

這將是這樣的:

<div class="pane table-head"> 
     <table> 
      <thead class=""> 
       <tr> 
        <th>Header</th> 
       </tr> 
      </thead> 
     </table> 
    </div> 
    <div class="pane table-body fill"> 
     <table> 
      <tbody> 
       <tr><td>Text</td></tr> 
       <tr><td>Text</td></tr> 
       ... 
      </tbody> 
     </table> 
    </div> 
    <div class="pane table-foot"> 
     <table> 
      <tfoot> 
       <tr> 
        <th>Footer</th> 
       </tr> 
      </tfoot> 
     </table> 
    </div> 

查看更新的jsfiddle:http://jsfiddle.net/bluecaret/va4HF/5/

這仍然不是一個理想的解決方案,我可能最終會拋棄這一點,並用一個div的法比奧的解決方案去雖然我可以根據一些測試看到潛在的問題。現在這就是我會堅持的。

+0

謝謝。它符合我的需求 – smenon

0

好,第一個問題是,你爲什麼要使用表所有,但以防萬一使用這樣的:

table, th{width:100% !important; min-width:240px; } 

其中最小寬度是表

然後最小寬度再次,你真的應該使用的div:少,更容易標記都沒有問題,像這樣的

+0

我使用的表格,因爲我有我需要插入的表格數據。如果你看小提琴,我會用其餘的窗格使用div。謝謝,但仍然無法正常工作。只是讓'th'爲最小寬度,而tbody仍然是短的 – BlueCaret

+0

啊,那麼你在聲明中添加tbody(和/或tr或td,如果你需要的話),認爲它只是第一個。此外,表格數據根本不需要表格,您可以使用div並應用「display:table」(請參閱​​http://www.w3schools.com/cssref/pr_class_display.asp上的完整示例)。我提到這一點,因爲如果你需要使用響應式佈局,它會拯救你的生命,相信我 – Devin

+0

仍然無法正常工作。它只能達到最小寬度並停止。我甚至將所有的表格元素添加到它和同樣的東西。而最小寬度100%也不起作用。查看更新的jsfiddle:http://jsfiddle.net/bluecaret/va4HF/3/我會研究使用Div的表格數據,但如果可能的話,我仍然傾向於使用表格,這使得我的團隊中的其他人更容易與在CSS中不懂的人一起工作。謝謝。 – BlueCaret