简体   繁体   English

冻结HTML表格列的宽度

[英]Freezing the width of an HTML table column

I'm trying to make a table with a fixed column, and i'm following this example: 我正在尝试制作一个具有固定列的表,并且我正在关注以下示例:

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

Container: 容器:

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

Fixed column: 固定栏:

.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*/
        }

The problem is that if the container has a fixed height, say 100px, the fixed column keeps it's default height without getting attached to the scrollbar. 问题在于,如果容器具有固定的高度,例如100px,则固定的列将保持其默认高度,而不会附加到滚动条。

Try adding an extra container, which you can set to relative positioning. 尝试添加一个额外的容器,您可以将其设置为相对位置。

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

You can see in the example below that the fixed rows scrolls vertically with the rest of the rows. 您可以在下面的示例中看到固定行与其余行垂直滚动。

http://jsfiddle.net/VRNsX/ http://jsfiddle.net/VRNsX/

If you aren't averse to a little jQuery, here is another approach I worked out: 如果您不喜欢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/ http://jsfiddle.net/YMvk9/786/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM