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

时间:2015-08-19 12:12:12

标签: javascript jquery html css html5

我一直在尝试使用固定标题(以及必要时的水平滚动条)创建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)。

感谢您的任何建议!

6 个答案:

答案 0 :(得分:1)

我猜你在css中需要这个:

table thead{position:fixed;}

enter image description here

答案 1 :(得分:0)

我认为你最好从容器中取出标题:

Caused by: java.lang.ClassNotFoundException:org.springframework.cglib.core.CodeGenerationException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
... 42 more

检查这个小提琴 - https://jsfiddle.net/byB9d/6241/

答案 2 :(得分:0)

我会分成2个部分并指定宽度(如果有响应,则为%)。

<table id="table-top">
<tr>
<th>A</th>
<th>B</th>
</tr>
</table>
<div id="table">
<table id="table_id">
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
</table>
</div>
CSS....

#table{
    overflow-y: auto; 
    height:150px;
    border-bottom:1px solid grey;
}
table{
    width:100%;
}
table td, table th{
    border-color: grey;
    border: 1px solid;  
    width:50%;
}

https://jsfiddle.net/tonytansley/mvp4u54q/

答案 3 :(得分:0)

使用这可能有助于你

<style>

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

    .container {
        position:relative;
        background:transparent;
        top:0px;

        overflow:auto;
        width: 100%;
        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:first-child div {
        border: none;
    }
    .wrap {
        width: 617px;
    }

    .wrap table {
        width: 600px;
        table-layout: fixed;
    }

      </style>
    </head>

    <body>
    <div class="wrap">
        <table class="head">
            <tr>
                <td>Table attribute name</td>
                <td>Value</td>
                <td>Description</td>
            </tr>
        </table>

    <div class="container">
        <table>

                <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>
        </table>
    </div>

    </div>

答案 4 :(得分:0)

找到这个小提琴......看起来很适合你:

使用一个部分来包装表[不是我的工作只是提供链接]

<section class="">

Fiddle

答案 5 :(得分:0)

https://jsfiddle.net/agpq359b/

不确定,这是你想要的

<div class="container">
<table>
    <thead>
        <tr class="header">
            <th>Table attribute name
            </th>
            <th>Value
            </th>
            <th>Description
            </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>

        </tbody>
    </table>
</div>

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

.container {
    background:transparent;
    overflow:auto;
    width: 100%;
    height: 200px;
}
table {
    border-spacing: 0;
    width:120%;
}

td, th {
    border-bottom:1px solid #eee;
    background: #ddd;
    color: black;
    padding: 9px 25px
}

th:nth-of-type(1), td:nth-of-type(1){
    width:200px;
}

th:nth-of-type(2), td:nth-of-type(2){
    width:100px;
}

th:nth-of-type(3), td:nth-of-type(3){
    width:200px;
}

td + td {
    border-left:1px solid #eee;
}

thead {
    position:absolute;
    border: none;
    top:0px;
}

tbody{
    margin-top:38px;
    display:block;
}