冻结HTML表格列的宽度

时间:2012-11-23 16:37:28

标签: html css fixed

我正在尝试使用固定列创建一个表,我正在遵循这个示例:

http://jsfiddle.net/emn13/YMvk9/

容器:

 div { 
            width: 600px; 
            overflow-x:scroll;  
            margin-left:5em; 
            overflow-y:visible;
            padding-bottom:1px;
        }

固定栏:

.headcol {
            position:absolute; 
            width:5em; 
            left:0;
            top:auto;
            border-right: 0px none black; 
            border-top-width:3px; /*only relevant for first row*/
            margin-top:-3px; /*compensate for top border*/
        }

问题在于,如果容器具有固定高度,例如100px,则固定列保持其默认高度而不会连接到滚动条。

1 个答案:

答案 0 :(得分:0)

尝试添加一个额外的容器,您可以将其设置为相对定位。

div#container{
    position:relative;
    height:100px;
    overflow:scroll;
}

您可以在下面的示例中看到固定行与其余行垂直滚动。

http://jsfiddle.net/VRNsX/

如果你不反对一个小jQuery,这是我解决的另一种方法:

$('#tbl').scroll(function(e) {
    var self = this;
    $('.headcol').each(function() {
        $(this).css({
            'top': ($(this).index('.headcol') * $(this).outerHeight() + 3) - $(self).scrollTop()
        });
    });
});

http://jsfiddle.net/YMvk9/786/