简体   繁体   English

如何使用ngAnimate平滑滚动?

[英]How to smooth scroll with ngAnimate?

I am wondering why inside my controller this does not work: 我想知道为什么在我的控制器里面这不起作用:

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');

    });
});

I am just trying to scroll smoothly to a specific offset to the top of the body tag. 我只是想平滑地滚动到身体标签顶部的特定偏移。 Do I have to use the ngAnimate in a different way? 我是否必须以不同的方式使用ngAnimate

TypeError: angular.element(...).find(...).animate is not a function TypeError: angular.element(...)。find(...)。animate不是一个函数

ngAnimate does not have anything to do with .animate() . ngAnimate.animate()没有任何关系。 This function is related to jQuery and not to AngularJS. 此函数与jQuery有关,而与AngularJS无关。 So a working solution will look like this: 因此,一个可行的解决方案将如下所示:

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. 另外,在加载上面的脚本之前,不要忘记在html文件中加载jquery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM