javascript - 提高大表上的 iScroll 性能

标签 javascript jquery iscroll

我正在根据用户滚动的方式以编程方式更新表格标题及其第一列位置以保持它们对齐。

我遇到的问题是,一旦我的数据集变得足够大,滚动就会变得越来越不稳定/不那么流畅。

相关代码在 fiddle 的最底部:

iScroll.on('scroll', function(){
    var pos = $('#scroller').position();
    $('#pos').text('pos.left=' + pos.left + ' pos.top=' + pos.top);

    // code to hold first row and first column
    $('#scroller th:nth-child(1)').css({top: (-pos.top), left: (-pos.left), position:'relative'});
    $('#scroller th:nth-child(n+1)').css({top: (-pos.top), position:'relative'});

    // this seems to be the most expensive operation:
    $('#scroller td:nth-child(1)').css({left: (-pos.left), position:'relative'});
});

我知道这可以通过缓存元素等来写得更有效。例如,我尝试将元素保存到数组中并以更“普通”的方式更新它们的位置:

headerElements[i].style.left = left + 'px'; // etc...

无论我进行回调的速度有多快,我仍然对结果不满意。你有什么建议吗?

https://jsfiddle.net/0qv1kjac/16/

最佳答案

只需使用 ClusterizeJS !它可以处理数十万行,正是为此目的构建的。

你问它是如何工作的?

The main idea is not to pollute DOM with all used tags. Instead of that - it splits the list to clusters, then shows elements for current scroll position and adds extra rows to top and bottom of the list to emulate full height of table so that browser shows scrollbar as for full list

关于javascript - 提高大表上的 iScroll 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43504715/

相关文章:

javascript - 删除数组中负的重复值

javascript - 获取外部内容(相同域)并将其插入到特殊的 div 中(无需替换)

javascript - 通过搜索id解析Json

android - 当软键盘出现在 phonegap 中时隐藏输入字段

html - iScroll 未在 jQuery Mobile 中的散列链接上加载

javascript - 这段代码如何查找两个对象数组之间的差异?

javascript - 如何处理 ajax 中的 php 回显消息。

javascript - 允许在不同屏幕尺寸的菜单上显示和隐藏的功能

javascript - 如何为 Bootstrap 模态窗口指定主要和次要操作?

javascript - 如何在手机间隙使用 iscroll javascript?