有时候在不分页且数据多的时候就需要滚动条,或者说是下拉加载的时候也需要滚动条,但是希望在滚动的时候数据的表头固定便于理解表体。
1. 第一种,在tbody元素上进行滚动
这种方式有个缺陷就是所有的单元格的宽度一样,平分整个tr的宽度,不会根绝内容自适应宽度。
如下:
table {
border-collapse: collapse;
width: 100%;
text-align: left;
border:1px solid #000;
}
td,th{
border:1px solid #000;
}
tbody{
display: block;
overflow-x: hidden;
overflow-y: auto;
height: 100px;
}
thead,tbody tr{
display: table;
width: 100%;
table-layout: fixed;
word-break: break-all;
}
table thead{
width: calc(100% - 17px);
}
表头1 | 表头2 | 表头3 | 表头4 | 表头5 |
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
结果:
有时候需要根据屏幕可用高度来动态设置表格的高度和表头的宽度:(也就是可用宽度不足以显示内容的时候将tbody定高,并且将thead的宽度减去滚动条的宽度使得上下对齐)
table {
border-collapse: collapse;
width: 100%;
text-align: left;
border: 1px solid #000;
}
td,
th {
border: 1px solid #000;
}
tbody {
display: block;
overflow-x: hidden;
overflow-y: auto;
}
thead,
tbody tr {
display: table;
width: 100%;
table-layout: fixed;
word-break: break-all;
}
表头1 | 表头2 | 表头3 | 表头4 | 表头5 |
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
$(function() {
var windowHeight = $(window).height(); //窗口可用高度
var topValue = $("#tebleDiv").offset().top;
var tebleDivHeight = $("#tebleDiv").height();
if (parseFloat(topValue) + parseFloat(tebleDivHeight) + 180 < parseFloat(windowHeight)) {
return;
}
var realHeight = windowHeight - topValue -50;
$("table tbody").css("height", realHeight + "px");
$("table thead").css("width", ($("table thead").width() - 18) + "px");
});
结果:
解释:
display:table 使得子元素平分父元素的宽度 (加在table上无用,必须针对tr设置)table-layout:fixed 在固定表格布局中,水平布局仅取决于表格宽度、列宽度、表格边框宽度、单元格间距,而与单元格的内容无关。
thead,tbody tr {
display: table;
width: 100%;
table-layout: fixed;
word-break: break-all;
}
table thead{
width: calc(100% - 17px);
}
为了使thead元素和tbody tr的宽度一致
tbody{
display: block;
overflow-x: hidden;
overflow-y: auto;
height: 100px;
}
将tbody元素以块级元素显示并且定高,超过高度自动显示滚动条。
2. 第二种:在table的父元素div元素上进行滚动,外层div滚动的时候表头固定
这种方式有个解决了上面的缺陷,但是有个问题是给人的效果不太平滑。重要代码是封装的JS方法:
table {
border-collapse: collapse;
width: 100%;
text-align: left;
border: 1px solid #000;
}
td,
th {
border: 1px solid #000;
background-color: #FFFFFF;
}
#tebleDiv {
overflow-x: hidden;
overflow-y: auto;
height: 120px;
}
表头1 | 表头2 | 表头3 | 表头4 | 表头5 |
---|---|---|---|---|
111111111111111111111111的高度发稿的放到大幅度放到官方刚发的的放到放到 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
1 | 2 | 3 | 4 | 5 |
var zdFixedTable = {
fixedTableHeadByTableDivId: function(divId) {
if (!divId) {
return;
}
var tableDiv = $("#" + divId);
var table = $("#" + divId + " table");
this.unbindDivScroll(tableDiv);
this.bindDivScroll(table, tableDiv);
},
setTheadWidth: function(table, tableDiv) {
if (!table || !tableDiv) {
return;
}
var tableBodyCell = table.find("tbody tr:first td");
var headerCell = table.find("thead tr th");
if (tableBodyCell && headerCell) {
headerCell.each(function(index) {
$(this).width(tableBodyCell.eq(index).width());
});
tableBodyCell.each(function(index) {
$(this).width(headerCell.eq(index).width());
});
}
},
unbindDivScroll: function(tableDiv) {
tableDiv.unbind("scroll");
},
bindDivScroll: function(table, tableDiv) {
zdFixedTable.setTheadWidth(table, tableDiv);
var timer;
tableDiv.scroll(function() {
clearTimeout(timer);
timer = setTimeout(function() {
var tableHead = table.find("thead");
var windowTop = tableDiv.scrollTop();
var position = tableHead.css("position");
if (windowTop > 5) {
if ("fixed" != position) {
tableHead.css("position", "fixed");
tableHead.css("margin-top", "-1px");
}
} else {
if ("static" != position) {
tableHead.css("position", "static");
tableHead.css("margin-top", "0px");
}
}
}, 200);
});
}
}
$(function() {
zdFixedTable.fixedTableHeadByTableDivId("tebleDiv");
});
效果:
文章链接
发表评论