简体   繁体   English

Angular:使用片段滚动锚点时出错

[英]Angular: error on anchor scrolling with fragments

So, I have this header.component.ts on my project, with links to all sections of the home page.所以,我的项目中有这个header.component.ts ,带有指向主页所有部分的链接。

        <nav class="nav-menu d-none d-lg-block">
        <ul>
            <li class="active"><a [routerLink]="['/']" [routerLinkActive]="'active'">Home</a></li>
            <li><a [routerLink]="['/']" fragment="portfolio" [routerLinkActive]="'active'">Portfólio</a></li>
            <li><a [routerLink]="['/']" fragment="services" [routerLinkActive]="'active'">Serviços</a></li>
            <li><a [routerLink]="['/']" fragment="contact" [routerLinkActive]="'active'">Contato</a></li>
        </ul>
    </nav>

I've changed my app.routing.module so anchor scrolling is enabled:我已经更改了我的app.routing.module以便启用锚滚动:

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {
      scrollPositionRestoration: 'enabled',
      anchorScrolling: 'enabled'
    })
  ],
  exports: [RouterModule]
})

Problem is: everytime I click on a link, instead of scrolling to the section with the related id , my page goes back to the top.问题是:每次我点击一个链接,而不是滚动到具有相关id的部分,我的页面回到顶部。 Is there anything that I am doing wrong?有什么我做错了吗?


Edit: I've created a stackblitz here , however its getting a compiler error编辑:我在这里创建了一个 stackblitz ,但是它得到了一个编译器错误

You have to add ids to your responsive sections since Angular will automatically scroll to the element with the id of the given fragment.您必须向响应部分添加 id,因为 Angular 将自动滚动到具有给定片段 id 的元素。

Got to the answer.得到了答案。 It turned out to be a problem on a script on the template I was using.原来是我使用的模板上的脚本有问题。 Once I removed the script (which was made to provide smooth the scrolling), my problem was solved.一旦我删除了脚本(这是为了提供平滑的滚动),我的问题就解决了。 Thank you @divin-irakiza and @chellappan-வ for the help!谢谢@divin-irakiza 和@chellappan-வ 的帮助!

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

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