我想在页面在某些条件下以 Angular 加载时向下滚动到某个部分。
请注意:我想向下滚动而不单击任何内容(即ngOnInit
(。
我试过这个: 在我的组件.html文件中
<div #sectionSubscribe>
HTML...
</div>
在我的组件.ts 文件中
ngOnInit(){
this.scrollToSubscribe(sectionSubscribe);
}
scrollToSubscribe($element){
$element.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'nearest'
});
}
但它不允许我这样做。 请帮忙
尝试如下:
@ViewChild("sectionSubscribe", { static: true }) sectionSubscribeDiv: ElementRef;
scrollToSubscribe(){
this.sectionSubscribeDiv.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
}
工作演示