【发布时间】:2017-12-22 14:43:47
【问题描述】:
我正在尝试创建一个带有滚动条的大型 HTML 表格(大约 5000 行),因此我考虑将该表格插入到 <div> 中,然后我可以随意格式化。
它在 Firefox 47 和 IE 11 中运行良好,但在 Chrome 59 中滚动时表现缓慢。
<!DOCTYPE html>
<html>
<head>
<title>Test page</title>
</head>
<body>
<div style="width: 400px; height: 300px; overflow: scroll;" id="test"></div>
<script>
let table = '<table style="table-layout: fixed; width: 3000px">';
table += '<thead>';
table += '<tr>';
for(let i=0; i < 30; i++) {
table += '<th style="width: 100px">#' + i +'</th>';
}
table += '</tr>';
table += '</thead>';
table += '<tbody>';
for(let i=0; i < 5000; i++) {
table += '<tr>';
for(let j=0; j < 30; j++) {
table += '<td>r: '+ i +' || c: '+ j +'</td>';
}
table += '</tr>';
}
table += '</tbody>';
table += '</table>';
document.getElementById('test').innerHTML = table;
</script>
</body>
</html>
但是,如果我只是将表格添加到文档正文中,滚动行为在我测试过的 3 个浏览器中是平滑的(但我只能在本地开发服务器中运行代码时观察到这种行为;如果我追加JSFiddle 中的表格到文档正文中 Chrome 的缓慢行为再次出现)。
我的问题是什么可能导致 Chrome 的这种迟缓行为,我该怎么做才能获得更流畅的滚动表?
*编辑:我已经从代码块中的<td> 中删除了style="position: relative",因为这就是我在 Fiddle 中所做的。我正在尝试这种风格,因为我注意到当表格元素相对定位时,IE 在滚动时往往会变得迟缓。我的最终目标是创建一个带有固定/冻结标题的大型 html 表格,该表格具有可滚动的主体和大量行。
【问题讨论】:
-
Chrome (64) 有同样的问题,而 FF 和 Edge 没有问题。但是,将“溢出:滚动”添加到外部 div - 就像在您的示例中一样 - 解决了这个问题。无论如何:看看 slickgrid,也许正是你想要的。
标签: html google-chrome html-table