Fixed table headers with a fixed banner div

若如初见. 提交于 2019-12-06 16:02:42

I came back to this problem after the weekend and solved it. I can't believe I couldn't see this last week! It proves what a fresh pair of eyes can do.
I thought I'd answer it here in case anyone else would like this code.


I changed one of the variables to listen to the top of the main section instead of the whole window:

var scroll = $(window).scrollTop();

Is now:

var scroll = $('#table-container').offset().top;


Then I changed the statement to call the function:

$(window).scroll(moveScroll);

Is now:

$('#table-container').scroll(moveScroll);


Lastly I manually set the top of the fixed header to 130px to match the bottom of the topsection.

Here's a fiddle: http://jsfiddle.net/hvRZy/


I still can't solve the border-collapse issue though so if anyone has any ideas on that front that'd be amazing. Thanks!


EDIT: I've managed to solve the border issue with the following CSS (also added rounded corners):

table {
    border-collapse: separate;
    border-spacing: 0px;
}
table tr th, table tr td {
    border-right: 1px solid #000;
    border-bottom: 1px solid #000;
    padding: 5px;
}
table tr th:first-child, table tr td:first-child {
    border-left: 1px solid #000;
}
table tr th {
    background: #c0c0c0;
    border-top: 1px solid #000;
    text-align: center;
}
table tr:first-child th:first-child {
    border-top-left-radius: 6px;
}
table tr:first-child th:last-child {
    border-top-right-radius: 6px;
}
table tfoot tr:last-child td:first-child {
    border-bottom-left-radius: 6px;
}
table tfoot tr:last-child td:last-child {
    border-bottom-right-radius: 6px;
}

One last fiddle! http://jsfiddle.net/KfxQg/

You can use jQuery TH Float Plugin. If you want to make table th to be fixed you can use it like this

$("#maintable").thfloat({
  attachment : "#maintable",
  noprint : false
});

FIDDLE

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!