我有一个带有标题的 HTML 表格,一旦用户向下滚动它,我就会使用 jQuery 将其锁定到浏览器的顶部。我已经在 SO 和其他地方看到了很多针对这个特定问题的解决方案,但无法克服我遇到的障碍。
我已经能够让我的 jQuery 很好地锁定标题(我的 中有一个 这很好用,向下滚动时将标题锁定到窗口顶部,向上滚动时将其恢复为默认值,但对于我来说,我无法将 < th > 和 < td > 列宽设置为添加以下 CSS 时排队: 如果我删除该样式,所有标题及其列都将完美对齐。此样式是必需的,不能删除。表格宽度也需要设置为 100%。有什么建议吗? jsFiddle here(如果删除最后 2 个 CSS 属性,您可以看到所需的行为,问题在 Firefox 中最为明显):http://jsfiddle.net/aKe6j/59/
最佳答案
试试这个(the fiddle),我认为这可能是解决方案: 关于javascript - 具有最大列宽和使用 jQuery 填充的 HTML 表固定标题,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/17025816/
包含我的列标题,并且我已经为它指定了一个 ID“theadTrId”)。
$(document).ready(function () {
var widths = new Array();
//create array of <th> widths
var i = 0;
$('th').each(function () {
widths[i] = $(this).width();
i++;
});
var elementPosTop = $('#theadTrId').offset().top;
$(window).scroll(function () {
SetWidths();
});
var SetWidths = function () {
var wintop = $(window).scrollTop();
if (wintop > elementPosTop) {
$('#theadTrId').css({ "position": "fixed", "top": "0" });
//<th> and <td> widths in 1st column
$('#th1').width(widths[0]);
$('#td1').width(widths[0]);
//<th> and <td> widths in 2nd column
$('#th2').width(widths[1]);
$('#td2').width(widths[1]);
//x number of other columns with same logic...
} else {
$('#theadTrId').css({ "position": "inherit" });
}
};
});
td {
max-width: 200px;
}
td, th {
padding: 10px;
}
<style>
table {
width: 100%;
}
td {
background-color: #BCE6E6;
}
#theadTrId {
background-color: #6699FF;
}
#th1 {
width: 110px;
}
#th2 {
width: 235px;
}
#th3 {
width: 80px;
}
td {
max-width: 200px;
}
td, th {
padding: 10px;
}
</style>
</head>
<table border="1">
<thead>
<tr id="theadTrId">
<th id="th1">Column 1</th>
<th id="th2">Column 2</th>
<th id="th3">Column 3</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<script>
//loop to add 40 rows
for (var i=0;i < 41; i++)
{
var jj = 1;
$("#tbody").append("<tr><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td><td id='td"+(jj++)+"'>Content " + i + "</td></tr>")
}
var widths = new Array();
//create array of <th> widths
var i = 0;
$('th').each(function () {
widths[i] = $(this).context.offsetWidth;
i++;
});
var elementPosTop = $('#theadTrId').offset().top;
$(window).scroll(function () {
SetWidths();
});
var SetWidths = function () {
var wintop = $(window).scrollTop();
if (wintop > elementPosTop) {
$('#theadTrId').css({ "position": "fixed", "top": "0" });
//use the css rulz to change also the max-width for that particular td
//it can be rolled back afther
//<th> and <td> widths in 1st column
$('#th1').css('width',widths[0]);
$('#td1').css('width',widths[0]);
$('#td1').css('max-width',widths[0]);
//<th> and <td> widths in 2nd column
$('#th2').css('width',widths[1]);
$('#td2').css('width',widths[1]);
$('#td2').css('max-width',widths[1]);
//<th> and <td> widths in 3rd column
$('#th3').css('width',widths[2]);
$('#td3').css('width',widths[2]);
$('#td3').css('max-width',widths[2]);
} else {
$('#theadTrId').css({ "position": "inherit" });
// if you need the max-width again
$('#td0').css('max-width',200);
$('#td1').css('max-width',200);
$('#td2').css('max-width',200);
}
};
</script>