$(window).scrollTop using angular.element($ window);在AngularJS中

时间:2016-05-22 12:53:03

标签: angularjs

我试图用JavaScript在AngularJS中模仿:

$(window).scrollTop(
    $scope.scrollPos[$location.path()] ? 
    $scope.scrollPos[$location.path()] : 0);

我知道我可以用var windowElement = angular.element($ window)替换$(window);

但有没有办法可以模拟scrollTop功能?

这里的参考是我使用的代码:

    $rootScope.scrollPos = {}; // scroll position of each view

    $rootScope.scrollClear = function (path) {
        $rootScope.scrollPos[path] = 0;
    }

    $rootScope.$on('$stateChangeStart',
        function (event, toState, toParams, fromState, fromParams) {
            $rootScope.okSaveScroll = false;
    });

    $rootScope.$on('$stateChangeSuccess',
        function (event, toState, toParams, fromState, fromParams) {
            var windowElement = angular.element($window);
            $timeout(function () { // wait for DOM, then restore scroll position
                windowElement.scrollTop($rootScope.scrollPos[$location.path()] ? $rootScope.scrollPos[$location.path()] : 0);
                $rootScope.okSaveScroll = true;
            }, 0);
        })

0 个答案:

没有答案