【bug】table从新加载数据,页面滚动条下沉到底部,记录scrollTop后将其恢复scrollTop出现闪烁

1.table数据请求前记录scrollTophtml

$scope.scrollPos = document.documentElement.scrollTop;

 

2.html中添加指令repeat-finishispa

<tr ng-repeat="item in lists" class="repeat-finish">

3.写入指令rest

AppDirective.directive('repeatFinish', function () {
    return {
        restrict: "C",
        link: function (scope, element, attr) {
            if(scope.$last === true){
                $(document).scrollTop(scope.scrollPos);
            }
        }
    }
});

当指令检测到DOM已经渲染好(scope.$last === true表示repeat渲染完成)当即恢复以前记录的scrollTop,这样从table加载好到恢复scrollTop就接近无延迟,闪烁的状况消失了。code

注:若是有滚动条定位误差(angular表达式编译完成呈现视图先后引发的折行,页面高度会发生变化,因此定位有差异。虽然scope.$last === true表示了遍历完成,可是页面重绘并无完成,主要是数据绑定的显示),在link里面加个setTimeout();这是会闪烁一下;htm

闪烁这个现象暂时没有找到完美的解决办法,有大佬想到什么办法的话,欢迎@,E-mail: dxdleikai@163.comblog

 

后续:(已经找到解决办法)

指令修改成以下:element

AppDirective.directive('repeatFinish', function ($parse) {
    return {
        restrict: "CA",
        link: function (scope, element, attr) {
            if(scope.$last === true){
                var watchLast = scope.$watch('lastHashKey', function(newValue, oldValue) {
                    $(document).scrollTop(scope.scrollPos);
                });
                scope.$on('$destroy', function() {
                    watchLast();
                });
            }
        }
    }
});

 

控制器须要在更新数据列表后添加:hash

$scope.lastHashKey = $scope.tableList[$scope.tableList.length - 1].$$hashKey)

完美解决it

相关文章
相关标签/搜索