一種區域性固定表頭的實現方案(橫向可跟隨內容一同滾動,縱向鎖定表頭)

江愛笛生發表於2019-02-16

1.為了固定表頭我們需要先把表格的head和tbody切分到兩個table裡: <table class=”table-head”></table><table class=”table-body”></table>裡。

2.將table-body放在一個table-body-box的div裡設定y軸方向可以滾動,x軸方向不可滾動,這樣表內容就能在自己的區域實現垂直方向的滾動了。
3.將table-head設定為absolute

4.將table-headtable-body-box的div放入一個table-box的div中,設定橫向x軸可以滾動,y軸不可滾動。
這樣兩個表就會在x方向滾動,y軸不滾動,表頭就垂直方向固定在垂直方向,橫向滾動在table-box出現滾動條的時候,內部的表格頭和表格內容因為沒有設定為fixed,所以會被滾動。

表頭和表內容的橫向方向滾動能力(其實是父級overflow-x:scroll樣式賦予的)。表內容的垂直方向滾動是通過表內容外套的table-body-box設定overflow-y:scroll來實現的。

5.程式碼如下:

<style>
.table-box {
    height: 100%;
    width: 100%;
    overflow-y: hidden;
    overflow-x: scroll;
    -webkit-overflow-scrolling: touch;
    position: relative;
}

.table-head {
    background-color: #F9FAFE;
    border-top: 1px solid #e8e8e8;
    border-bottom: 1px solid #e8e8e8;
    color: #777;
    table-layout: fixed;
    position: absolute;
    top:0;
}

.table-head th {
    border-collapse: collapse;
    height: 40px;
}

.table-body-box {
    table-layout: fixed;
    -webkit-overflow-scrolling: touch;
    overflow-x: hidden;
    overflow-y: scroll;
    height: 100%;
    box-sizing: border-box;
    padding-top: 50px;
}

.table-body{
    table-layout: fixed;
    width: 100%;
    background-color: white;
    text-align: center;
}
.table-body tr:nth-child(even) {
    background-color: #F9FAFE;
}
</style>


html如下
        <div style="height:500px">
            <div class="table-box">
                <table class="table-head"  style="width: 900px">
                    <colgroup>
                        <col width="150">
                        <col width="100">
                        <col>  
                        <col>
                        <col>
                        <col>
                        <col>
                    </colgroup>
                    <thead>
                    <tr>
                        <th>Company</th>
                        <th>Provision</th>
                        <th>Following in Point</th>
                        <th>Customer Grade</th>
                        <th>Allocated Time</th>
                        <th>Follow up Person</th>
                        <th>Department</th>
                    </tr>
                    </thead>
                </table>
                <div class="table-body-box" style="width: 900px">
                    <table class="table-body">
                        <colgroup>
                            <col width="150">
                            <col width="100">
                            <col>  
                            <col>
                            <col>
                            <col>
                            <col>
                        </colgroup>
                        <tbody>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        <tr>
                            <td>Shanghai Yinghuang</td>
                            <td>FOB</td>
                            <td>Dumped Cabinet</td>
                            <td>Important</td>
                            <td>2017-10-28</td>
                            <td>Nathan</td>
                            <td>Sales Department</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
   

6.可以結合flex做到自適應表頭和表格內容

相關文章