AngularJS如何滚动到指定元素并防止浏览器跳转

4

我编写了一个应用程序,其中包含一系列块。

每个块都包含指向其他块的链接。当点击指向块的链接时,例如# /home/page-19,页面根据当前位置向下/向上动画。

这一切目前都可以正常工作,但当单击锚点并更新路由时,浏览器会跳转到顶部,然后再执行动画向下,我需要它从当前位置开始执行动画。

我编写了一个指令,将preventDefault添加到所有锚点。

请参见以下JS Fiddle。 http://jsfiddle.net/ygNWF/10/

简化代码:

HTML:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>

<body ng-app="app" ng-controller="appController">
    <script type="text/ng-template" id="home-template">
       <div class="page" id="page-17" >
              <div class="page-inner" ng-style="style()" resize><a href="#/home/page-18">Page 18</a></div>
          </div>
          <div class="page" id="page-18">
              <div class="page-inner" ng-style="style()" resize><a href="#/home/page-19">Page 19</a></div>
          </div>
          <div class="page" id="page-19">
              <div class="page-inner" ng-style="style()" resize><a href="#/home/page-20">Page 20</a></div>
          </div>
          <div class="page" id="page-20">
              <div class="page-inner" ng-style="style()" resize><a href="#/home/page-17">Page 17</a></div>
          </div>
    </script>

    <div class="wrapper">
      <div class="view" ng-view scroll></div>
    </div>
</body>

javascript:

var app = angular.module('app', []);

/*
  Controllers
*/
app.controller( 'appController', function( $scope ) {

});
app.controller( 'routeController', function( $scope, $routeParams ) {
  //When route change, check if page has been updated and run animateScroll directive
  if(typeof $routeParams !== 'undefined' && $routeParams.page.length > 0){
    $scope.animateScroll($routeParams.page);
  }
});

/*
  Page routes
*/
app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/home/:page', {
        templateUrl: 'home-template',
        controller: 'routeController'
      })

      .otherwise({
        redirectTo: '/home/'
      });
}]);

/*
  Directives
*/
app.directive('scroll', function ($routeParams, $timeout) {
  /* 
  Scroll to element ID
  */
  return {
    restrict: 'A',
    link: function(scope, elm, attrs) {
      scope.animateScroll = function(page) {
        $timeout(function(){
          console.log('test');
          if(jQuery('#' + page).length == 1){
            jQuery('html, body').animate({
              scrollTop:  jQuery('#' + page).position().top
            }); 
          };
        }, 1);
      };
    }
  };
});

app.directive('resize', function ($window) {
  return function (scope, element) {
    var w = angular.element($window);

    scope.getWindowDimensions = function () {
      return { height: w.height() };
    };

    scope.$watch(scope.getWindowDimensions, function (dimensions) {
        scope.windowHeight = dimensions.height;
        scope.style = function () {
          return { 
              'min-height': scope.windowHeight + 'px'
          };
        };
    }, true);

    w.bind('resize', function () {
      scope.$apply();
    });
  }
});

app.directive('a', function() {
  return {
    restrict: 'E',
    link: function(scope, elem, attrs) {
      elem.on('click', function(e){
        e.preventDefault();
      });
    }
  };
});

页面布局示例:

enter image description here

1个回答

5
我不确定滚动是否应该与路由混合在一起。我更倾向于如下的方式: 1. 使用ng-click而不是href,并且使用 href="javascript:" (如果要支持IE8,则必须这样做) 2. 使用接收元素id的滚动函数。
以下是一个示例:http://jsfiddle.net/naS4U/

我有这种感觉。唯一的问题是,我需要URL链接到页面。 - CharliePrynn
啊,好的,也许看起来不是很好,但你可以在重定向到新的URL之前存储当前滚动位置,并在重定向后立即将滚动移动到该位置,然后平滑地动画滚动到所选页面。 - IgorCh
我刚遇到了这个问题。我使用了href="javascript:void()" title="#idvalue"的方式,这样你就可以在旧浏览器中检索属性标题,并根据需要应用它。 - C. S.
3
@C.S.,最好不要这样使用title属性,因为在悬停状态下会显示“#idvalue”,请尝试使用data-link属性或其他data-*属性。 - IgorCh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接