jjzjj

javascript - ngSmoothScroll Angular Directive(指令)不起作用

coder 2024-07-26 原文

我正在使用以下指令 https://github.com/d-oliveros/ngSmoothScroll使这个项目中的东西平滑地滚动到选定的元素。

这是我的代码:

...
<!-- build:js(.) scripts/vendor.js -->
    <!-- bower:js -->
    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
    <script src="bower_components/angular-animate/angular-animate.js"></script>
    <script src="bower_components/angular-aria/angular-aria.js"></script>
    <script src="bower_components/angular-cookies/angular-cookies.js"></script>
    <script src="bower_components/angular-messages/angular-messages.js"></script>
    <script src="bower_components/angular-resource/angular-resource.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/angular-touch/angular-touch.js"></script>
    <!-- endbower -->
    <!-- endbuild -->
    <script src="bower_components/ngSmoothScroll/dist/angular-smooth-scroll.min.js"></script>
        <!-- build:js({.tmp,app}) scripts/scripts.js -->
        <script src="scripts/app.js"></script>
        <script src="scripts/controllers/initcontroller.js"></script>
        <!-- endbuild -->
        <script src="scripts/libs/materialize.min.js"></script>
        <script src="scripts/libs/angular-materialize.js"></script>
</body>
...

这就是包含脚本的地方 (~/angular-smooth-scroll.min.js) 在我的 app.js 文件中我有:

angular
  .module('sccateringApp', [
    'ngAnimate',
    'ngAria',
    'ngCookies',
    'ngMessages',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.materialize',
    'smoothScroll'
  ])

'smoothScroll' 是项目的实际依赖项。按照本文开头给出的链接中的说明,这就是我在我的观点中所做的:

<li><a href="#" scroll-to="service-info" container-id="service-info">Contáctame</a></li>
...

<section class="service-info" id="service-info">
<h1 class="sofia-font">Detalles de Servicio</h1>
...

但是,没有发生平滑滚动,而且控制台或 jslint 在我的 grunt serve 任务中没有给出警告/错误。

知道我可能做错了什么吗?我是 Angular 的新手,我仍在努力寻找解决方法。

最佳答案

我不确定,但这可能是因为您在链接上使用了 container-id 而不是非 anchor 标记。我正在使用它来滚动到页脚中的元素。我的代码是:

//the anchor link in my nav
 <a href="#" scroll-to="footer" duration="2500">Click Me</a>
 ...

//the element I want to scroll to
 <div id="footer"></div>
 ...

缩小版不适合我,所以我的脚本如下:

<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/angular-animate/angular-animate.js"></script>
<script src="/bower_components/angular-aria/angular-aria.js"></script>
<script src="/bower_components/angular-messages/angular-messages.js"></script>
<script src="/bower_components/angular-material/angular-material.js"></script>
<script src="/bower_components/angular-sanitize/angular-sanitize.js"></script>

<script src="/bower_components/ngSmoothScroll/lib/angular-smooth-scroll.js"></script>

<script src="/js/app.module.js"></script>
<script src="/js/app.controller.js"></script>
<script src="/js/app.service.js"></script>

对于模块:

angular
   .module('glasser', [
      'ngMaterial',
      'ngSanitize',
      'smoothScroll'
    ])

关于javascript - ngSmoothScroll Angular Directive(指令)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33245287/

有关javascript - ngSmoothScroll Angular Directive(指令)不起作用的更多相关文章

随机推荐