HTMLページテーブルスクロールバーの実装

不言
リリース: 2018-05-08 15:14:23
オリジナル
5018 人が閲覧しました

この記事では、主に HTML ページでのテーブル スクロール バーの実装について紹介します。これは、必要な友人に参考にしていただけるよう、共有します。

予定のページがたくさんあります。データ量が多すぎると、水平および垂直スクロールバーが生成されます。ユーザーが見やすくするために、メーターのヘッダーを修正する必要があります。この要件に基づいて、この機能を実装するためのデモが作成されました。

主にいくつかの問題を解決します:
  • 1.テーブルは水平および垂直スクロールバーを実装します
  • 2.テーブルヘッダーは修正されました
  • 3.テーブルの列幅の適応
  • 4 .テーブルコンテンツラップしません
  • メインコードブロック
css:

            .table-scroll {
                width: calc(100% - 5px);
                overflow-x: scroll;
                white-space: nowrap;
            }

            .table-scroll table {
                table-layout: fixed;
                width: calc(100% - 10px);
                background-color:darkseagreen ;
            }

            .table-scroll thead {
                display: table-row;
                background-color: bisque;
            }

            .table-scroll tbody {
                overflow-y: scroll;
                overflow-x: hidden;
                display: block;
                height: calc(100vh - 300px);
            }

            .table-scroll th,td {
                width: 160px;
                overflow: hidden;
                text-overflow: ellipsis;
                min-width: 160px;
                border: 1px solid #808080;
            }

            h4, h5 {
                color: cornflowerblue;
            }
ログイン後にコピー

js:

    $(function() {
            $('.table-scroll').scroll(function() {
                  $('.table-scroll table').width($('.table-scroll').width() 
                  + $('.table-scroll').scrollLeft());
                });

                var tableTdWidths = new Array();
            var tableWidth = 0;
            var tableTr0Width = 0;
            var tableThNum = 0;
            var tableTr1Width = 0;

                tableWidth = $('.table-scroll table').css('width').replace('px','');
                tableThNum = $('.table-scroll tr:eq(0)').children('th').length;            if ($('.table-scroll tr').length == 1) { // header only                if (tableWidth > tableTr0Width) {
                    $('.table-scroll tr:eq(0)').children('th').each(function(i){
                        $(this).width(parseInt(($(this).css('width').replace('px','')) 
                        + parseInt(Math.floor((tableWidth - tableTr0Width) / tableThNum))) + 'px');
                    });
                }
            } else { // header and body
                tableTr1Width = $('.table-scroll tr:eq(1)').css('width').replace('px','');
                    $('.table-scroll tr:eq(1)').children('td').each(function(i){
                    tableTdWidths[i]=$(this).css('width').replace('px','');
                });
                $('.table-scroll tr:eq(0)').children('th').each(function(i) {            if(parseInt($(this).css('width').replace('px', '')) >
                parseInt(tableTdWidths[i])) {
                tableTdWidths[i] = $(this).css('width').replace('px','');
                    }
                });                if (tableWidth > tableTr1Width) {
                    //set all th td width
                    $('.table-scroll tr').each(function(i){
                            $(this).children().each(function(j){
                                $(this).css('min-width',(parseInt(tableTdWidths[j]) 
                                + parseInt(Math.floor((tableWidth - tableTr1Width) / 
                                tableThNum))) + 'px');
                            });
                    });
                } else {
                    //method 1 : set all th td width
                    $('.table-scroll tr').each(function(i){
                            $(this).children().each(function(j){
                                $(this).css('min-width',tableTdWidths[j] + 'px');
                            });
                    });
                }
            }
            });
ログイン後にコピー

html:

    <body>
        <h4>完成效果:1.固定表头 2.table横纵滚动条 3.table列宽自适应 4.table内容不换行</h4>
        <p class="table-scroll">
            <table>
                <thead>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                    <th>title1</th>
                </thead>
                <tbody>
                    <tr>
                        <td>1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>3</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>4</td>
                        <td>body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1 body1 body1 body1 body1 body1 body1 body1 body1 body1 body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>5</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>6</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>7</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>8</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>9</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>10</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>11</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>12</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>13</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>14</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>15</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>16</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>17</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>18</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>19</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>20</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>21</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>22</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>23</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>24</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>25</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>26</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>27</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>28</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>28</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>29</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                    <tr>
                        <td>30</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                        <td>body1</td>
                    </tr>
                </tbody>
            </table>
        </p>
    </body>
ログイン後にコピー
インターネット上には多くの例がありますが、効果はあなたが望むものではないため、独自に行う必要があります仕事、写真は見栄えがよくありません、効果に焦点を当ててください、問題があれば修正してください。

関連する推奨事項:


HTMLページのネイティブVIDEOタグの非表示ダウンロードボタン機能

HTMLページのロード後に実行するjsメソッドを実装します

以上がHTMLページテーブルスクロールバーの実装の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート