AngularJS + Safari: Force page to scroll to top when pages switch

后端 未结 9 1894
深忆病人
深忆病人 2021-02-07 00:20

I am having an odd, safari-only scrolling behavior using AngularJS.

Whenever the user flips between pages, the pages are being changed as if they are AJAX. I understand

相关标签:
9条回答
  • 2021-02-07 00:33

    Call $window.scrollTo(0,0); after locationChangeSuccess event:

    $rootScope.$on("$locationChangeSuccess",
    function(event, current, previous, rejection) {
      $window.scrollTo(0,0);
    });
    
    0 讨论(0)
  • 2021-02-07 00:33

    you can use this:

    .run(["$rootScope", "$window", '$location', function($rootScope, $window,  $location) {
    
        $rootScope.$on('$routeChangeStart', function(evt, absNewUrl, absOldUrl){
            $window.scrollTo(0,0);    //scroll to top of page after each route change
    }}])
    

    or for tab switches you can use the $window.scrollTo(0,0); in your controller

    0 讨论(0)
  • 2021-02-07 00:44

    Have you tried using $anchorScroll()? it's documented here.

    0 讨论(0)
  • 2021-02-07 00:45

    You can use $anchorScroll

    $scope.gotoTop = function (){
      // set the location.hash to the id of
      // the element you wish to scroll to.
      $location.hash('top');
    
      // call $anchorScroll()
      $anchorScroll();
    };
    
    0 讨论(0)
  • 2021-02-07 00:48

    $window.scrollTo(0,0) will scroll to the top of the page.

    I just found a nice plugin (pure angularJS) that supports animations also:

    https://github.com/durated/angular-scroll

    0 讨论(0)
  • 2021-02-07 00:49

    I got the same problem while using AngularJS in a Cordova App. In a normal Browser or on Android i have no trouble but on ios i get the same behavior as discribed by Neil.

    The AngularJS documentation on $anchorScroll is not that great so i thought to post this link which helped me way more:

    http://www.benlesh.com/2013/02/angular-js-scrolling-to-element-by-id.html

    0 讨论(0)
提交回复
热议问题