我试图通过使用 position:relative 来固定在顶部位置,
$(document).ready(function() {
var container = $(".container");
container.css("overflow-y", "auto");
container.css("overflow-x", "hidden");
container.on("scroll", function() {
$("table", this).css("overflow-x", "hidden");
$("th", this).css("position", "relative");
$("th", this).css("z-index", 1);
$("th", this).css("top", this.scrollTop + "px");
});
});
问题示例 https://jsfiddle.net/hth4Ly7q/2/ 它在 chrome、firefox 中工作得很好,但 Safari 和 IE 在滚动时滞后,也许有人知道如何在 safari 中修复它? 我没有使用绝对/固定位置,因为我的真实表格非常复杂,无法即时计算列宽。
最佳答案
我在 Edge 和 IE 上试过了。看起来不错。 https://jsfiddle.net/55cynfLg/
function setTh(){
var container = $(".container");
$("table", this).css("overflow-x", "hidden");
$("th", this).css("z-index", 1);
container.find("th").each(function(i){
var td = container.find("tbody tr:eq(0) td:eq("+i+")");
$(this).css({
"width": td.width(),
"left": td.offset().left
});
});}
关于javascript - 滚动的顶部位置导致 Safari/IE 滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41736680/