Table header to stay fixed at the top when user scrolls it out of view with jQuery

后端 未结 25 2541
悲&欢浪女
悲&欢浪女 2020-11-22 05:38

I am trying to design an HTML table where the header will stay at the top of the page when AND ONLY when the user scrolls it out of view. For example, the table may be 500

25条回答
  •  迷失自我
    2020-11-22 06:22

    I was able to fix the problem with changing column widths. I started with Andrew's solution above (thanks so much!) and then added one little loop to set the widths of the cloned td's:

    $("#header-fixed td").each(function(index){
        var index2 = index;
        $(this).width(function(index2){
            return $("#table-1 td").eq(index).width();
        });
    });
    

    This solves the problem without having to clone the entire table and hide the body. I'm brand new to JavaScript and jQuery (and to stack overflow), so any comments are appreciated.

提交回复
热议问题