doulian8554 2014-10-20 10:36
浏览 97

即使在滚动之后也使表标题的位置固定

I have a table with a vertical scroll bar

<div id="div_data" style="top: 7px;">
<table id="data" cellspacing="0" cellpadding="0">
            <thead>
              <tr class="fix"><td class="newClass"> <div> Algebra </div></td>
                   <td class="newClass"><div> Geometry</div></td>
                   <td class="newClass"><div> Theorems</div></td> 
                   <td class="newClass"><div> Comment</div></td>
                   </tr>
            </thead>
            <tbody style="overflow:auto; height:100px;">
            ......................
            </tbody>

    </table>
        <table id="header-fixed" cellspacing="0" cellpadding="0"></table> 
    </div>      

When I scroll the #data table, I need to make the table heading elements to be fixed without scrolling.For that I have done the following jquery code:

var tableOffset = $("#data").offset().top;
var $header = $("#data > thead").clone();
var $fixedHeader = $("#header-fixed").append($header);  
    $("#div_data").scroll(function(){ 
        $('#div_row_headers').scrollTop($('#div_data').scrollTop()); 
         var offset = $(this).scrollTop();

        if (offset >= tableOffset && $fixedHeader.is(":hidden")) {
            $fixedHeader.show();
        }
        else if (offset < tableOffset) {
            $fixedHeader.hide();
        } 
    });

The content inside the element of the first table is cloned inside the second table,but the position of the #header-fixed table is not on the heading region of the first table.It is coming in the middle of the table.I just want to superimpose it on the position of the elements in the #data table.My css is below:

#header-fixed { 
position: fixed; 
display:none;
background-color:white;
}
table#header-fixed tr td {
    width: 155px;
}       
table#header-fixed tr td div {
    width: 155px;
    float: left;
    text-align: center;
}   

EDIT: When I scroll the windows scroll bar,the new table is fixed,I need to scroll the table,when I scroll the window scroll bar

  • 写回答

1条回答 默认 最新

  • doudao2954 2014-10-20 10:45
    关注

    The only thing you have to do is that wrap your table body using tbody tag. And apply following css to that tag.

     tbody {
        display: block;
        height: 100px;
        overflow: auto;        
        width: 100%;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况