HTML table表頭固定(自己做項(xiàng)目使用的幾種方法)

還記得之前公司需要做表頭固定,但是由于基礎(chǔ)太差,去查了好多好多文檔。最后總結(jié)出幾個(gè)不錯(cuò)的方法和插件。

一、使用css + js來實(shí)現(xiàn)表頭固定

項(xiàng)目demo

使用css定位th 根據(jù)父級(jí)滾動(dòng)條scrolltop的偏移量獲取值,在用js把偏移量賦值到th的定位top上。就做到了表頭固定。(此方法需要固定高度)

項(xiàng)目demo

css樣式部分 主要是出現(xiàn)滾動(dòng)條和定位th還有固定高度。

<style>
    .table-responsive {
        overflow: auto !important;
    }
    .table-th-css {
        background: #EFEFF4 !important;
        position: relative !important;
        text-align: center;
        top: 0;
    }
    .section-scroll{
        height:417px;
    }
</style>
  

html部分 自己做肯定內(nèi)容超級(jí)多 demo我就不復(fù)制那么多內(nèi)容了。

<div class="table-responsive section-scroll">
    <table class="table table-bordered">
        <thead class="table-header">
            <tr>
                        <th class="table-th-css">
                            <div>部門</div>
                        </th>
                        <th class="table-th-css">
                            <div>用戶名稱</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>1月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>2月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>3月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>4月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>5月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>6月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>7月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>8月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>9月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>10月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>11月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>12月</div>
                        </th>
                        <th class="text-center table-th-css">
                            <div>合計(jì)</div>
                        </th>
            </tr>
        </thead>
        <tbody >
            <tr class="text-center" >
                        <td >
                            多毛大叔愛蘿莉
                        </td>
                        <td class="table-textWidth">
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                           多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                           多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                           多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                           多毛大叔愛蘿莉
                        </td>
                        <td>
                           多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
                        <td>
                            多毛大叔愛蘿莉
                        </td>
            </tr>
        </tbody>
    </table>
</div>

js內(nèi)容 使用jq的on事件,監(jiān)聽滾動(dòng)根據(jù)我自己項(xiàng)目的樣式修改了下自己的樣式。大家使用可自行調(diào)整。

var tableCont = $('.section-scroll tr th'); //獲取th
var tableCont_child = $('.section-scroll tr th div'); //獲取th下邊的div
var tableScroll = $('.section-scroll'); //獲取滾動(dòng)條同級(jí)的class

        function scrollHandle() {
            var scrollTop = tableScroll.scrollTop();
            // 當(dāng)滾動(dòng)距離大于0時(shí)設(shè)置top及相應(yīng)的樣式
            if (scrollTop > 0) {
                tableCont.css({
                    "top": scrollTop + 'px',
                    "marginTop": "-1px",
                    "padding": 0
                });
                tableCont_child.css({
                    "borderTop": "1px solid gainsboro",
                    "borderBottom": "1px solid gainsboro",
                    "marginTop": "-1px",
                    "padding": "8px"
                })
            } else {
            // 當(dāng)滾動(dòng)距離小于0時(shí)設(shè)置top及相應(yīng)的樣式
                tableCont.css({
                    "top": scrollTop + 'px',
                    "marginTop": "0",
                });
                tableCont_child.css({
                    "border": "none",
                    "marginTop": 0,
                    "marginBottom": 0,
                })
            }
        }
tableScroll.on('scroll', scrollHandle);

這樣第一種方式的表頭固定就完成了。在瀏覽器上看著基本沒瑕疵,但是我用mui使用這種方法,可能是app的滾動(dòng)有回彈所以效果會(huì)顯得有點(diǎn)卡頓。本人菜雞不喜勿噴(歡迎回復(fù)..)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,204評(píng)論 1 92
  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,239評(píng)論 3 119
  • 作為語文老師,最痛苦的事是什么?不是備課,不是授課,不是公開課,不是批作業(yè),而判作文、作文、作文!為什么呢?因?yàn)椋?..
    胡喜平閱讀 340評(píng)論 0 0
  • 1. 咪蒙有一篇流傳甚廣的文章一一《致賤人》,這篇文章里出現(xiàn)的“賤人”都有一個(gè)共同的特點(diǎn),理直氣壯地要求別人幫忙,...
    然歲閱讀 4,118評(píng)論 35 121
  • 價(jià)值人生 life198012 股票型基金和混合型基金里面有被動(dòng)型和主動(dòng)型。被動(dòng)型的包括指數(shù)基金、策略基金等等,此...
    bonjour1218閱讀 286評(píng)論 0 2

友情鏈接更多精彩內(nèi)容