How to retain scroll position of ng-repeat in AngularJS?

前端 未结 9 1433
梦谈多话
梦谈多话 2020-12-05 00:47

DEMO

List of objects is rendered using ng-repeat. Suppose that this list is very long and user is scrolling to the bottom to see some objects.

W

9条回答
  •  春和景丽
    2020-12-05 01:11

    You can solve this problem with ng-animate:

    .animation('.keep-scroll', [function () {
        var keepScroll = function(element, leave){
            var elementPos = element.offset().top;
            var scrollPos = document.body.scrollTop;
    
            if(elementPos < scrollPos){
                var height = element[0].clientHeight;
                if(leave){
                    height *= (-1);
                }
                document.body.scrollTop += height;
            }
        };
    
        return {
            enter: function (element, doneFn) {
                keepScroll(element);
                doneFn();
            },
            leave: function (element, doneFn) {
                keepScroll(element, true);
                doneFn();
            }
        };
    }])
    

    Just assign the css-class .keep-scroll to your repeated elements, like this:

    ...

提交回复
热议问题