【问题标题】:Scroll inside the table在表格内滚动
【发布时间】:2015-01-21 01:52:25
【问题描述】:

我在页面中有一个简单的 html 表格,我只需要让这个表格可以从内部滚动并保持表格标题不变。

我还需要让这个表格适合整个屏幕,这样页脚才可见。我不需要指定静态高度,因为此屏幕将以高分辨率显示,并且以像素为单位应用特定高度毫无意义。

这是小提琴示例:

http://jsfiddle.net/3L4zb7cf/

代码:

    <div class="container">
        <table class="containertbl">
            <tr>
                <td>
                    <div class="header">
                        This is the Header for the website.
                        This is the Header for the website.
                        This is the Header for the website.
                        This is the Header for the website.
                        This is the Header for the website.
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="content">
                        <table cellspacing="0" cellpadding="0">
                            <thead>
                                <tr>
                                    <th>col1</th><th>col2</th><th>col3</th><th>col4</th><th>col5</th><th>col6</th><th>col7</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td></tr>
                                <tr><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td></tr>
                                <tr><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td></tr>
                                <tr><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td></tr>
                                <tr><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td></tr>
                                <tr><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td><td>details 1</td></tr>
                            </tbody>
                        </table>
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="footer">
                        This is the Footer for the website.
                        This is the Footer for the website.
                        This is the Footer for the website.
                        This is the Footer for the website.
                        This is the Footer for the website.
                    </div>
                </td>
            </tr>
        </table>
    </div>

CSS:

        .header {
            background-color: navy;
            color: white;
            padding: 20px;
        }

        .footer {
            background-color: navy;
            color: white;
            padding: 20px;
        }

        .content{
            padding: 7px;
        }
        .content table {
            width: 100%;
            height: 100%;
        }

        .content table thead tr th{
            color: aliceblue;
            background-color: darkgoldenrod;
            border: 1px solid #98bf21;
        }

        .content table tbody tr td{
            text-align: center;
            border: 1px solid #98bf21;
        }

        .tbl{
        }

        .container{
            width: 100%;
            background-color: lightblue;
        }

        .containertbl {
            height: 100%;
        }

        body{
            margin: 0;
            font-family: cursive;
        }

【问题讨论】:

    标签: html css scroll html-table


    【解决方案1】:

    要使表头固定,您应该更改

    position : fixed;
    

    但在您的情况下,它与其他 css 属性冲突。所以我建议为表头创建另一个表并让它处于固定位置。

    试试这个小提琴:

    jsFiddle

    【讨论】:

    • 我的意思是“表格”标题,这样只有“页面”标题是固定的,但表格标题和页脚不是。
    • 然后将表头的位置更改为固定。让我更新答案。
    • 我知道这种方法并尝试过。仍然没有提供我想要达到的输出。您可以更新小提琴示例以显示您的解决方案。这里的问题是滚动后表格标题将隐藏在页眉下。有没有办法让表头也固定?
    • 我认为您应该为标题另取一张桌子。并使其位置固定。试试这个小提琴。 jsfiddle.net/codeSpy/uu1x8262 如果是解决方案,我会更新答案。
    • 您的答案很有帮助,但其他答案对其他访问者会更有用。所以竖起大拇指!
    【解决方案2】:

    添加

            height: 200px;
            overflow: auto;
    

    .content 类中。 升级小提琴: http://jsfiddle.net/3L4zb7cf/1/

    编辑: http://jsfiddle.net/3L4zb7cf/4/

    在第二个例子中,有了这些属性,我认为你可以做你想做的事:

    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    

    【讨论】:

    • 我提到我不需要指定内容的高度,因为它应该在屏幕上显示。
    • 我达到了那么远,但无法保持表格标题不变。我想知道这在技术上是否可行
    • mmh,没有任何 javascript?我觉得任务会很艰巨。
    • 修复标题可以通过其他表在另一个之上完成,但已修复。这不是一个干净的解决方案,但仍然有效。
    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 2011-05-30
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多