简体   繁体   English

具有垂直固定标题的XY滚动HTML表

[英]XY Scroll HTML table with vertically fixed header

I want to add scroll to my html table, both horizontally and vertically too. 我也想在水平和垂直方向上将滚动条添加到我的html表中。 But only tbody should be allowed to scroll, and thead should be fixed while scrolling tbody vertically and scrolled/adjusted while scrolling tbody horizontally. 但是只允许tbody滚动,垂直滚动tbody时应固定thead,水平滚动tbody时应滚动/调整。 I know that I have to apply overflow css property to tbody and then after it, adjust thead width with offset by using jquery. 我知道我必须将溢出css属性应用于tbody,然后再通过使用jquery来调整thead的宽度和偏移量。 If anyone knows the solution, then answer will be appreciated. 如果有人知道解决方案,那么答案将不胜感激。 Here is my code. 这是我的代码。

HTML Part HTML部分

<table>
<thead>
    <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
        <th>Column 4</th>
        <th>Column 5</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Row 1</td>
        <td>Row 1</td>
        <td>Row 1</td>
        <td>Row 1</td>
        <td>Row 1</td>
    </tr>
    <tr>
        <td>Row 2</td>
        <td>Row 2</td>
        <td>Row 2</td>
        <td>Row 2</td>
        <td>Row 2</td>
    </tr>
    <tr>
        <td>Row 3</td>
        <td>Row 3</td>
        <td>Row 3</td>
        <td>Row 3</td>
        <td>Row 3</td>
    </tr>
    <tr>
        <td>Row 4</td>
        <td>Row 4</td>
        <td>Row 4</td>
        <td>Row 4</td>
        <td>Row 4</td>
    </tr>
    <tr>
        <td>Row 5</td>
        <td>Row 5</td>
        <td>Row 5</td>
        <td>Row 5</td>
        <td>Row 5</td>
    </tr>
    <tr>
        <td>Row 6</td>
        <td>Row 6</td>
        <td>Row 6</td>
        <td>Row 6</td>
        <td>Row 6</td>
    </tr>
    <tr>
        <td>Row 7</td>
        <td>Row 7</td>
        <td>Row 7</td>
        <td>Row 7</td>
        <td>Row 7</td>
    </tr>
    <tr>
        <td>Row 8</td>
        <td>Row 8</td>
        <td>Row 8</td>
        <td>Row 8</td>
        <td>Row 8</td>
    </tr>
    <tr>
        <td>Row 9</td>
        <td>Row 9</td>
        <td>Row 9</td>
        <td>Row 9</td>
        <td>Row 9</td>
    </tr>
    <tr>
        <td>Row 10</td>
        <td>Row 10</td>
        <td>Row 10</td>
        <td>Row 10</td>
        <td>Row 10</td>
    </tr>
</tbody>
</table>

CSS 的CSS

        html {
            font-family: verdana;
            font-size: 10pt;
            line-height: 25px;
        }
        table {
            border-collapse: collapse;
            width: 300px;
            overflow-x: scroll;
            display: block;
        }
        thead {
            background-color: #EFEFEF;
        }
        thead, tbody {
            display: block;
        }
        tbody {
            overflow-y: scroll;
            overflow-x: hidden;
            height: 140px;
        }
        td, th {
            min-width: 100px;
            height: 25px;
            border: dashed 1px lightblue;
        }

JS JS

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
    <script type="text/javascript">
        $('table').on('scroll', function () {
            $("table > *").width($("table").width() + $("table").scrollLeft());
        });
    </script>

http://codepen.io/EasonWang01/pen/bEjRvj try it. http://codepen.io/EasonWang01/pen/bEjRvj尝试一下。

html {
        font-family: verdana;
        font-size: 10pt;
        line-height: 25px;
    }
    table {
        border-collapse: collapse;
        width: 540px;

        display: block;
    }
    thead {
        background-color: #EFEFEF;
    }
    thead, tbody {
        display: block;
    }
    tbody {
     overflow-y: scroll;
        height: 140px;
    }
    td, th {
        min-width: 100px;
        height: 25px;
        border: dashed 1px lightblue;
    }

Done. 做完了 As it was my mistake to start jquery function. 因为启动jquery函数是我的错误。 By putting js code into $(function () {}); 通过将js代码放入$(function () {}); , it is solved. ,就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM