我想要实现的是点击并平滑滚动到底部/指定的div区域,我用hashtag定义就像我认为它应该是这样的.
这是w3school示例中为JQuery编写的实例:https://www.w3schools.com/jquery/tryit.asp ? filename = tryjquery_eff_animate_smoothscroll
我做的是从这个答案中窥视:Angular2使用Hashtag路由到页面锚点
但我真的不明白答案,答案看起来像这样:
这部分是HTML部分:
<a [routerLink]="['somepath']" fragment="Test">Jump to 'Test' anchor </a>
Run Code Online (Sandbox Code Playgroud)
在这之下,router.navigate是我应该把代码放在哪里?component.ts对吗?但我如何访问此功能?我应该实施(点击)?
this._router.navigate( ['/somepath', id ], {fragment: 'test'});
Run Code Online (Sandbox Code Playgroud)
在这之下,我得到它,它应该写在我的component.ts:
** Add Below code to your component to scroll**
import {ActivatedRoute} from '@angular/router'; // <-- do not forget to import
private fragment: string;
constructor(private route: ActivatedRoute { }
ngOnInit() {
this.route.fragment.subscribe(fragment => { this.fragment = fragment; });
}
ngAfterViewInit(): void {
try {
document.querySelector('#' + this.fragment).scrollIntoView();
} catch (e) { }
}
Run Code Online (Sandbox Code Playgroud)
什么是"somepath"是什么意思?我应该在我的路线中添加路线.对吗?通常,我在这里添加一个新的路径,例如: …