在某些情况下在Angular中加载页面时,我想向下滚动到某个部分。
请注意:我想向下滚动而不单击任何东西(即ngOnInit
)。
我试过了:在我的component.html文件中
<div #sectionSubscribe>
HTML...
</div>
在我的component.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" });
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句