javascript - 试图在 HTML 中制作固定的表头、滚动和可见性问题?

标签 javascript jquery css html

我一直在尝试用 HTML 制作一个带有固定标题(必要时还有水平滚动条)的表格。这个 JSFiddle 显示了我目前所拥有的:

html, body {
    margin:0;
    padding:0;
    height:100%;
}

.horizontalscrollcontainer {
    position: relative;
    padding-top: 37px;
    overflow:auto;
}

.verticalscrollcontainer {
    background:transparent;
    overflow-y:auto;
    overflow-x:visible;
    height: 200px;
}
table {
    border-spacing: 0;
    width:120%;
}
td + td {
    border-left:1px solid #eee;
}
td, th {
    border-bottom:1px solid #eee;
    background: #ddd;
    color: black;
    padding: 10px 25px;
}
th {
    height: 0;
    line-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    color: transparent;
    border: none;
    white-space: nowrap;
}
th div {
    position: absolute;
    background: transparent;
    color: black;
    padding: 9px 25px;
    top: 0px;
    margin-left: -25px;
    line-height: normal;
    border-left: 1px solid #800;
}
th:first-child div {
    border: none;
}

<div class="horizontalscrollcontainer">
    <div class="verticalscrollcontainer">
        <table>
            <thead>
                <tr class="header">
                    <th>Table attribute name
                        <div>Table attribute name</div>
                    </th>
                    <th>Value
                        <div>Value</div>
                    </th>
                    <th>Description
                        <div>Description</div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>align</td>
                    <td>left, center, right</td>
                    <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
                </tr>
                <tr>
                    <td>bgcolor</td>
                    <td>rgb(x,x,x), #xxxxxx, colorname</td>
                    <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
                </tr>
                <tr>
                    <td>border</td>
                    <td>1,""</td>
                    <td>Specifies whether the table cells should have borders or not</td>
                </tr>
                <tr>
                    <td>cellpadding</td>
                    <td>pixels</td>
                    <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
                </tr>
                <tr>
                    <td>cellspacing</td>
                    <td>pixels</td>
                    <td>Not supported in HTML5. Specifies the space between cells</td>
                </tr>
                <tr>
                    <td>frame</td>
                    <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
                    <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
                </tr>
                <tr>
                    <td>rules</td>
                    <td>none, groups, rows, cols, all</td>
                    <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
                </tr>
                <tr>
                    <td>summary</td>
                    <td>text</td>
                    <td>Not supported in HTML5. Specifies a summary of the content of a table</td>
                </tr>
                <tr>
                    <td>width</td>
                    <td>pixels, %</td>
                    <td>Not supported in HTML5. Specifies the width of a table</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

https://jsfiddle.net/byB9d/6248/

我遇到的问题是水平滚动条似乎在 verticalscrollcontainer 而不是 horizo​​ntalscrollcontainer 上。有什么方法可以调节这个(overflow-x 自动设置为 auto 因为 overflow-y 设置为 auto)。

感谢您的任何建议!

最佳答案

我猜你在 css 中需要这个:

table thead{position:fixed;}

Fiddle

关于javascript - 试图在 HTML 中制作固定的表头、滚动和可见性问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32095180/

相关文章:

html - 浏览器什么时候添加 CSS 样式作为属性,即使这些没有明确指定?

html - 文本转换样式不起作用

javascript - jQuery:自动点击链接?

javascript - 根据屏幕宽度裁剪单词

javascript - Elasticsearch https cors 已启用,但仍然收到 No 'Access-Control-Allow-Origin' header is present on the requested resource

javascript - Jquery 悬停显示和隐藏可见性问题

javascript - 通过XHR获取json数据以动态智能渲染dhtmlxgrid无限循环

javascript - bxslider - 幻灯片未使用 JQuery 正确刷新

javascript - 使用通用参数的 AJAX 调用始终提供 readState = 1

javascript - 在全屏视频上放置文本 (HTML5)