javascript - 如何使用 angularJS 制作虚拟卷轴?

标签 javascript angularjs performance directive virtualscroll

我试图制定一个指令,我可以做一个虚拟滚动,所以当用户滚动表格时,表格删除“旧” View 并添加"new" View ,有点像收集重复,但我已经一直失败,我想我不明白它背后的数学原理,有人可以帮助我吗?

这是我的指令代码:

BaseModule.directive('myScroll', function() {
    return {
        restrict:"A",
        scope:{
            rows:"=",
            headers:"="
        },
        link: function(scope,el) {
            var scrollTop = 0;
            var scrollLeft = 0;
            angular.element(el).on('scroll',function(){
                scrollTop = $(el).scrollTop();
                scrollLeft = $(el).scrollLeft();
                $(el).parent().find(".header").scrollLeft(scrollLeft);
                var height = $(el).height();
                var numberOfRows = height/23;
                var initialRow = scrollTop/23;
                var html = "";
                for(i=0; i<numberOfRows;i++){
                    var row = scope.rows[i+initialRow];
                    html = html + addRow(row,i+initialRow);
                }
                $(el).find('.tbody-scroll').append(html);
            });
            scope.$watch('rows',function(rows){
                var height = $(el).height();
                var numberOfRows = height/23;
                var initialRow = scrollTop/23;
                var html = "";
                for(i=0; i<numberOfRows;i++){
                    var row = rows[i+initialRow];
                    html = html + addRow(row,i+initialRow);
                }
                $(el).find('.tbody-scroll').append(html);
            });
            var addRow = function(row,index){
                var html = "";
                var pos = 0;
                var totalWidth = 0;
                angular.forEach(row,function(col){
                    var width = scope.headers[pos].width;
                    totalWidth = totalWidth + width;
                    html = html + "<span style='width:"+width+"px'>"+col.value+"</span>";
                    pos++;
                });
                html = "<div class='row' style='top:"+index*23+"px;width:"+totalWidth+"px;'>"+html;
                html = html + "</div>";
                return html;
            };
        }
    };
});

<!-- my directive .html -->
<div class="mTable">
    <div class="header" ng-style="headerWidth(headers())">
        <span ng-repeat="header in headers()" ng-style="widthStyle(header)">
            {{::header.label}}
        </span>
    </div>
    <div class="tbody-container" my-scroll headers="headers()" rows="rows()">
        <div class="tbody-scroll" ng-style="scrollHeight(rows(),headers())"></div>
    </div>
</div>

最佳答案

用代码给出完整的答案可能需要付出太多的努力
这个库在 ng-repeat https://github.com/stackfull/angular-virtual-scroll 上实现虚拟滚动在描述中还有一篇关于如何自己实现此功能的文章。

基本概念是制作两个 div,一个在列表上方,一个在列表下方,其大小由列表中元素的数量决定(这种方法有一个限制,因为列表元素必须具有相同的高度或它们的高度必须固定),然后当元素从视口(viewport)中消失时删除它们,并根据当前未呈现的元素数量和您在列表中的位置调整 div 的大小

关于javascript - 如何使用 angularJS 制作虚拟卷轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33259241/

相关文章:

javascript - JS - 获取点击标签的数据属性以在其他页面上使用

javascript - js-routes ASP.NET MVC 替代方案

javascript - Angular js : Unable to watch a variable in service and notify to controller : Possible 2 rootscopes

jquery - Angularjs jqliteappend() 和 jqueryappend() 与 <td> 的行为不同

python - 如何测量python函数的速度

javascript - 将 Content MathML 转换为 javascript 中的中缀/数学符号字符串

javascript - Angular 变量未绑定(bind)到 View

html - 匿名表格单元格 - 它们会影响回流/重绘性能吗?

performance - JavaFX - IDE 和可执行 jar 性能

javascript - 在 jquery UI 对话框上使用 select2 (v 4.0) 时,如何让 allowClear 选项在使用远程数据源时起作用?