純html+css固定表格一列或多列,其餘進行橫向滾動的方法-鵬仔先生

純html+css固定表格一列或多列,其餘進行橫向滾動的方法

純html+css固定表格一列或多列,其餘進行橫向滾動的方法

    工作時候,遇到一個需求,表格的前兩列固定不動,其他的跟隨滾動條滾動,並沒有在意,想起來很簡單,剛開始想法是前兩列absolute定位,後面的relative定位並給left值爲前面的總寬度,就這樣做了,發現是有問題的,具體啥問題就不多說了,如真想研究,自己嘗試吧。

 

    當然,網上有很多插件的,比較好用,也建議大家使用插件吧,如TableFreeze-master

插件下載

藍奏網盤:點擊下載

 

    下面給大家分享一下我的另個想法,純html+css實現:

    就用固定一列做個簡單的demo,外面最大的盒子“layer-box”加定位relative,裏面的盒子“layer”加滾動條,接下來就是表格的樣式了,給第一列“td-1”需要固定的添加定位absolute,並設置固定寬度,z-index屬性,那麼他就默認定位到起始位置了。接下來是重點,我們需要多加一列來佔位,“<td><div class="zhanwei"></div></td>”中的div設置需要固定的第一列的寬度,如設有邊框,記得減掉,通過他佔位,將後面所有的列都向後擠了一定的距離,就好了,大體思路就是這樣。

    如果固定兩列,那麼第二列的left值等於第一列的寬度,佔位的寬度等於前面所有固定列的寬度和即可。

    當然,你也可以讓最後一列或多列固定,方法一樣,給最後一列的前一列進行佔位,思路相反想一下。

附完整demo

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>固定第一列</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            padding: 100px;
            box-sizing: border-box;
        }
        table{
            border-collapse: collapse;
        }
        td,th{
            color: #333;
            font-size: 14px;
            border-right: 1px solid #333;
            line-height: 30px;
            box-sizing: border-box;
            white-space: nowrap;
            background: #fff;
        }
        .layer-box{
            width: 700px;
            overflow: hidden;
            position: relative;
            border: 1px solid #000;
        }
        .layer{
            width: 700px;
            overflow: hidden;
            overflow-x: scroll;
        }
        .td-1{
            position: absolute;
            left: 0;
            z-index: 2;
            width: 150px;
        }
        .zhanwei{
            width: 149px;
        }
        table tr:nth-child(odd) td,
        table tr:nth-child(odd) th{
            background: #f7f8f9;
        }
    </style>
</head>
<body>
    <div class="layer-box">
        <div class="layer">
            <table>
                <tr>
                    <th class="td-1">我是第一個</th>
                    <th><div class="zhanwei"></div></th>
                    <th>我是第二個</th>
                    <th>我是第三個</th>
                    <th>我是第四個</th>
                    <th>我是第五個</th>
                    <th>我是第六個</th>
                    <th>我是第七個</th>
                    <th>我是第八個</th>
                    <th>我是第九個</th>
                    <th>我是第十個</th>
                </tr>
                <tr>
                    <td class="td-1">我是第一個</td>
                    <td><div class="zhanwei"></div></td>
                    <td>我是第二個</td>
                    <td>我是第三個</td>
                    <td>我是第四個</td>
                    <td>我是第五個</td>
                    <td>我是第六個</td>
                    <td>我是第七個</td>
                    <td>我是第八個</td>
                    <td>我是第九個</td>
                    <td>我是第十個</td>
                </tr>
                <tr>
                    <td class="td-1">我是第一個</td>
                    <td><div class="zhanwei"></div></td>
                    <td>我是第二個</td>
                    <td>我是第三個</td>
                    <td>我是第四個</td>
                    <td>我是第五個</td>
                    <td>我是第六個</td>
                    <td>我是第七個</td>
                    <td>我是第八個</td>
                    <td>我是第九個</td>
                    <td>我是第十個</td>
                </tr>
                <tr>
                    <td class="td-1">我是第一個</td>
                    <td><div class="zhanwei"></div></td>
                    <td>我是第二個</td>
                    <td>我是第三個</td>
                    <td>我是第四個</td>
                    <td>我是第五個</td>
                    <td>我是第六個</td>
                    <td>我是第七個</td>
                    <td>我是第八個</td>
                    <td>我是第九個</td>
                    <td>我是第十個</td>
                </tr>
            </table>
        </div>
    </div>
</body>
</html>

個人博客 http://sharedblog.cn/post/178.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章