angular: how to make link to jump for certain section in the same page

最后都变了- 提交于 2019-11-29 12:48:53

install this package
ng2-page-scroll

after import in your app.module.ts

    import {Ng2PageScrollModule} from 'ng2-page-scroll';

    @NgModule({
        imports: [
            /* Other imports here */
            Ng2PageScrollModule
            ]
    })

   export class AppModule {
   }

and test in your component html

<a pageScroll href="#home">Testing</a>
<div id="home">

try this

this.route.fragment.subscribe(fragment => { 
                      setTimeout(() => {
                            this.scrollTo(fragment);
                        }, 300);
     });

Use fragment as you did and bind to a click event like so:

in the html

<a fragment="account-about" (click)="navigateToSection('account-about')>About</a>

<div id="account-about">...</div>

in the ts

public navigateToSection(section: string) {
      window.location.hash = '';
      window.location.hash = section;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!