如何使用ngAnimate平滑滚动?
问题描述:
我很奇怪,为什么我的控制器内,这并不工作:如何使用ngAnimate平滑滚动?
angular.module('app', [
'ngAnimate',
])
.controller('MainCtrl', function ($scope, $log, $window, $document) {
var scrollTop = 200 // For example
angular.element(document).find('body').animate({scrollTop: scrollTop}, 'slow');
});
});
我只是想平滑地滚动到特定的一个偏移body标签的顶部。我需要以不同的方式使用ngAnimate
吗?
TypeError: angular.element(...).find(...).animate is not a function
答
ngAnimate
没有什么关系.animate()
。这个函数与jQuery相关,而不是与AngularJS相关。因此,一个工作解决方案将如下所示:
angular.module('app', [
'ngAnimate',
])
.controller('MainCtrl', function ($scope, $log, $window, $document) {
var scrollTop = 200 // For example
$('html, body').animate({scrollTop: scrollTop}, 'slow');
});
});
Also do not forget to load jquery in your html file before you load the script above.