>  기사  >  웹 프론트엔드  >  HTML 페이지 테이블 스크롤 막대 구현

HTML 페이지 테이블 스크롤 막대 구현

不言
不言원래의
2018-05-08 15:14:234995검색

이 글에서는 주로 HTML 페이지의 테이블 스크롤바 구현에 대해 소개합니다. 이제 모든 사람과 공유합니다. 도움이 필요한 친구들이 참조할 수 있습니다.

테이블 스크롤바 및 헤더 수정됨

페이지가 너무 많습니다. 데이터 양이 너무 많으면 가로, 세로 스크롤바가 생성됩니다. 사용자가 쉽게 볼 수 있도록 측정기 헤더를 수정해야 합니다. 이 요구 사항에 따라 이 기능을 구현하기 위한 데모가 작성되었습니다.

  • 주로 여러 가지 문제를 해결합니다.

  • 1.table은 가로 및 세로 스크롤 막대를 구현합니다.

  • 2.table 헤더 고정

  • 3.table 열 너비 적응형

  • 4 .table 내용 래핑하지 않음

Main code block

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.