具有可滚动行和固定标题的表

爱奥努特

我有基于此解决方案的代码,但它似乎对我不起作用。

我希望能够:

  • 滚动行
  • 标头要固定
  • 标头宽度与第一TD相同

我对JavaScript很陌生,所以我不知道自己缺少什么。滚动部分正在工作,但不在jsfiddle中。如何使TH宽度与相同TD

编辑1:

<html>
<head>
    <style>
        .outerDIV {
            position: relative;
            padding-top: 30px;   //height of your thead
        }
        .innerDIV {
            overflow: auto;
            max-height: 200;       //the actual scrolling container
        }
        table thead {
            display: block;
            position: absolute;
            top: 0;
            left: 0;
        }
        table tbody {
            display: block;
        }
    </style>
    <script>
        function scrollingTableSetThWidth(tableId)
        {
            var table = document.getElementById(tableId);

            ths = table.getElementsByTagName('th');
            tds = table.getElementsByTagName('td');

            if(ths.length > 0) {
                for(i=0; i < ths.length; i++) {
                    ths[i].style.width = getCurrentComputedStyle(tds[i], 'width');
                }
            }
        }

        function getCurrentComputedStyle(element, attribute)
        {
            var attributeValue;
            if (window.getComputedStyle) 
            { // class A browsers
                var styledeclaration = document.defaultView.getComputedStyle(element, null);
                attributeValue = styledeclaration.getPropertyValue(attribute);
            } else if (element.currentStyle) { // IE
                attributeValue = element.currentStyle[vclToCamelCases(attribute)];
            }
            return attributeValue;
        }
    </script>

</head>
<body>
    <div class="outerDIV">
        <div class="innerDIV">
            <table border="1">
                <thead>
                    <tr>
                        <div><th>Column1</th><th>Column2</th><th>Column3</th></div>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Line1Cell1</td><td>Line1Cell2</td><td>Line1Cell3</td>
                    </tr>
                    <tr>
                        <td>Line2Cell1</td><td>Line2Cell2</td><td>Line2Cell3</td>
                    </tr>
                    <tr>
                        <td>Line3Cell1</td><td>Line3Cell2</td><td>Line3Cell3</td>
                    </tr>
                    <tr>
                        <td>Line4Cell1</td><td>Line4Cell2</td><td>Line4Cell3</td>
                    </tr>
                    <tr>
                        <td>Line5Cell1</td><td>Line5Cell2</td><td>Line5Cell3</td>
                    </tr>
                    <tr>
                        <td>Line6Cell1</td><td>Line6Cell2</td><td>Line6Cell3</td>
                    </tr>
                    <tr>
                        <td>Line7Cell1</td><td>Line7Cell2</td><td>Line7Cell3</td>
                    </tr>
                    <tr>
                        <td>Line8Cell1</td><td>Line8Cell2</td><td>Line8Cell3</td>
                    </tr>
                    <tr>
                        <td>Line9Cell1</td><td>Line9Cell2</td><td>Line9Cell3</td>
                    </tr>
                    <tr>
                        <td>Line10Cell1</td><td>Line10Cell2</td><td>Line10Cell3</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>
西里尔

您需要给width元素设置一个absolute位置。

为了更好看,宽度应减小约1 em(滚动条的平均宽度)。

在此处输入图片说明

如果所有列的宽度均匀分布,则可以执行以下操作:http : //jsfiddle.net/MVxZb/9/

.outerDIV {
    position: relative;
    padding-top: 30px;
    display:inline-block;
}
.innerDIV {
    overflow: auto;
    max-height: 150px;
    padding-right:1.1em;
    margin-right:-1.1em;
}
table thead {
display:table;
    width:100%;
    border:solid 1px gray;
    box-sizing:border-box;
    position: absolute;
    border-bottom:0;
    top: 0;
    left: 0;
    right:0;
}

table-layout:fixed;如果未设置th和tds的宽度,则可以用来在列上均匀分布宽度。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

具有固定标题的可滚动div内容区域

来自分类Dev

具有自适应列宽的可滚动固定标题 html 表

来自分类Dev

CSS:具有多个表头的固定标题的滚动表

来自分类Dev

CSS:具有多个表头的固定标题的滚动表

来自分类Dev

带有固定子标题的固定标题和下面的可滚动内容

来自分类Dev

如何为固定标题和可滚动表编写CSS

来自分类Dev

如何为固定标题和可滚动表编写CSS

来自分类Dev

带有固定标题和可滚动主体的表格内的工具提示

来自分类Dev

具有固定标题的UWP垂直滚动条

来自分类Dev

具有固定标题的水平和垂直滚动内容

来自分类Dev

具有水平滚动内容的固定标题

来自分类Dev

具有响应表宽度的HTML表固定标题

来自分类Dev

浮动在TH上方的DIV用于具有固定标题的滚动表,但我无法将标题居中

来自分类Dev

具有固定标题的Android Listview表

来自分类Dev

CSS和DIV,标题和正文具有可滚动的内容,右侧固定了侧边栏

来自分类Dev

HTML表具有固定的页眉和页脚以及可滚动的正文,没有固定的宽度

来自分类Dev

具有固定列和标题的RecyclerView,以及可滚动的页脚

来自分类Dev

带有固定标题的两个可滚动列设计

来自分类Dev

带有固定标题且正文没有滚动的 Bootstrap 表

来自分类Dev

表格可滚动时无法固定标题

来自分类Dev

具有固定标题的滚动视图(android)setCanceledOnTouchOutside不起作用

来自分类Dev

嵌入式表垂直滚动并带有固定标题

来自分类Dev

具有固定/左列和可滚动正文的HTML表?

来自分类Dev

具有固定标题/标题的内联div

来自分类Dev

固定的标题和可滚动的正文

来自分类Dev

具有固定标题和全页宽度网格的Gridview

来自分类Dev

具有固定标题和表格响应引导程序的垂直对齐中间

来自分类Dev

创建具有固定标题和可变列宽的表格的最简单的方法是什么?

来自分类Dev

带有固定标题和固定列的HTML表格

Related 相关文章

  1. 1

    具有固定标题的可滚动div内容区域

  2. 2

    具有自适应列宽的可滚动固定标题 html 表

  3. 3

    CSS:具有多个表头的固定标题的滚动表

  4. 4

    CSS:具有多个表头的固定标题的滚动表

  5. 5

    带有固定子标题的固定标题和下面的可滚动内容

  6. 6

    如何为固定标题和可滚动表编写CSS

  7. 7

    如何为固定标题和可滚动表编写CSS

  8. 8

    带有固定标题和可滚动主体的表格内的工具提示

  9. 9

    具有固定标题的UWP垂直滚动条

  10. 10

    具有固定标题的水平和垂直滚动内容

  11. 11

    具有水平滚动内容的固定标题

  12. 12

    具有响应表宽度的HTML表固定标题

  13. 13

    浮动在TH上方的DIV用于具有固定标题的滚动表,但我无法将标题居中

  14. 14

    具有固定标题的Android Listview表

  15. 15

    CSS和DIV,标题和正文具有可滚动的内容,右侧固定了侧边栏

  16. 16

    HTML表具有固定的页眉和页脚以及可滚动的正文,没有固定的宽度

  17. 17

    具有固定列和标题的RecyclerView,以及可滚动的页脚

  18. 18

    带有固定标题的两个可滚动列设计

  19. 19

    带有固定标题且正文没有滚动的 Bootstrap 表

  20. 20

    表格可滚动时无法固定标题

  21. 21

    具有固定标题的滚动视图(android)setCanceledOnTouchOutside不起作用

  22. 22

    嵌入式表垂直滚动并带有固定标题

  23. 23

    具有固定/左列和可滚动正文的HTML表?

  24. 24

    具有固定标题/标题的内联div

  25. 25

    固定的标题和可滚动的正文

  26. 26

    具有固定标题和全页宽度网格的Gridview

  27. 27

    具有固定标题和表格响应引导程序的垂直对齐中间

  28. 28

    创建具有固定标题和可变列宽的表格的最简单的方法是什么?

  29. 29

    带有固定标题和固定列的HTML表格

热门标签

归档