gpt4 book ai didi

html - 谷歌浏览器中大型(ish)html 表格的缓慢滚动行为

转载 作者:太空狗 更新时间:2023-10-29 14:20:19 25 4
gpt4 key购买 nike

我正在尝试创建一个带有滚动条的大型 HTML 表格(大约 5000 行),所以我考虑将该表格插入到 <div> 中。然后我可以随意格式化。

它在 Firefox 47 和 IE 11 中运行良好,但在 Chrome 59 中滚动时表现缓慢。

WORKING DEMO

<!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 的这种缓慢行为,我该怎么做才能获得更流畅的滚动表?

*编辑:我删除了 style="position: relative"来自 <td>在代码块中,因为这就是我在 fiddle 中所做的。我正在尝试这种风格,因为我注意到当表格元素相对定位时,IE 倾向于在滚动时变得迟缓。我的最终目标是创建一个带有固定/卡住标题的大型 html 表格,该表格具有包含大量行的可滚动主体。

最佳答案

尝试将 transform: translate3d(0, 0, 0) 添加到表中。下面给出了一个例子。然而,这个技巧已经过时了。关于它的帖子可以追溯到 2012 年。例如,参见 this posting .

Currently, browsers [...] ship with hardware acceleration; they only use it when they have an indication that a DOM element would benefit from it. With CSS, the strongest indication is that a 3D transformation is being applied to an element.

在我的公司,我们几乎将它用于每个较大的列表。大多数情况下,它工作正常。另一种解决方案是虚拟化。

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;
document.getElementById('test2').innerHTML = table;
#test {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
<h2>With translate3d</h2>
<div style="width: 400px; height: 300px; overflow: scroll;" id="test"></div>
<h2>Without translate3d</h2>
<div style="width: 400px; height: 300px; overflow: scroll;" id="test2"></div>

(或 full snippet)

关于html - 谷歌浏览器中大型(ish)html 表格的缓慢滚动行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45154788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com