在某些情况下,我想在 Angular 中加载页面时向下滚动到某个部分。
请注意:我想向下滚动而不单击任何内容(即ngOnInit)。
我尝试了这个:在我的component.html文件中
<div #sectionSubscribe>
HTML...
</div>
Run Code Online (Sandbox Code Playgroud)
在我的 component.ts 文件中
ngOnInit(){
this.scrollToSubscribe(sectionSubscribe);
}
scrollToSubscribe($element){
$element.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'nearest'
});
}
Run Code Online (Sandbox Code Playgroud)
但它不允许我这样做。请帮忙
尝试这样:
@ViewChild("sectionSubscribe", { static: true }) sectionSubscribeDiv: ElementRef;
scrollToSubscribe(){
this.sectionSubscribeDiv.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1152 次 |
| 最近记录: |