Can't figure out why page loads at bottom? Angular UI-Router autoscroll Issue

前端 未结 5 2040
眼角桃花
眼角桃花 2020-12-13 14:18

For the life of me, I cannot figure out why this home page loads at the bottom. Is this an angular ui-router, angular, javascript or CSS issue? I\'ve been stuck on this fo

相关标签:
5条回答
  • 2020-12-13 14:57

    I ran into this issue a while ago. What is happening is you are on page 1, scrolled down the page I imagine, then click a button to go to page 2. The router then doesn't readjust your scroll position when you go to the next page. I fixed this by scrolling to top on all route changes.

    0 讨论(0)
  • 2020-12-13 15:05

    in the run block add:

    $rootScope.$on('$stateChangeSuccess', function () {
      $anchorScroll();
     });
    

    Of course you have to inject the $anchorScroll dependency

    0 讨论(0)
  • 2020-12-13 15:13

    here is a possible solution, i used a workoround, since auto scrolling wasnt working for me, so i forced my view to scroll to top. Hope this helps.

    app.run(['$window', '$rootScope', '$location' ,'$cookieStore', '$state', 'CacheManager',  '$timeout', function($window, $rootScope, $location, $cookieStore, $state,CacheManager, $timeout){
    
    
    $rootScope.$on('$viewContentLoaded', function(){
    
    var interval = setInterval(function(){
      if (document.readyState == "complete") {
          window.scrollTo(0, 0);
          clearInterval(interval);
      }
    },200);
    
    });
    
    
    
    }]);
    
    0 讨论(0)
  • 2020-12-13 15:17

    In your App routing module, Add the following to RouterModule

    RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled'})
    
    0 讨论(0)
  • 2020-12-13 15:19

    Angular UI-Router recently updated it's app so that it automatically scrolls down to new views loaded by default. This was causing my app's pages to load scrolled down. To turn this off simply add the following attribute to your ui-view:

    <div ui-view="header" autoscroll="true"></div>
    
    0 讨论(0)
提交回复
热议问题