我有一个带有标题的HTML表格,一旦用户向下滚动浏览器,我就会使用jQuery锁定到浏览器的顶部。我已经在SO和其他地方看到了很多针对这个特定问题的解决方案,但是无法克服我遇到的障碍。
我已经能够让我的jQuery锁定标题了(我的< tr>在我的< thead>中包含我的列标题,我已经给了< tr>一个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" });
}
};
});
这样可以正常工作并在向下滚动时将标题锁定到窗口顶部,并在向上滚动时将其恢复为默认值,但是对于我的生活,我无法得到&lt; th>和&lt; td&gt;添加以下CSS时要排列的列宽:
td {
max-width: 200px;
}
td, th {
padding: 10px;
}
我应该删除该样式,所有标题及其列都完美排列。此样式是必需的,无法删除。表宽度也需要设置为100%。有什么建议吗?
jsFiddle here(如果删除最后2个CSS属性,可以看到所需的行为,问题在Firefox中最为明显):http://jsfiddle.net/aKe6j/59/
答案 0 :(得分:3)
试试这个(the fiddle),我认为它可以是解决方案:
<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>