在Angular中如何在ngOnInit中向下滚动?

14 浏览
0 Comments

在Angular中如何在ngOnInit中向下滚动?

我想在某个条件下,在Angular中在页面加载时滚动到某个部分。

请注意:我想在不点击任何东西的情况下(即在ngOnInit中)滚动下去。

我尝试过这样做:

在我的component.html文件中

    HTML...

在我的component.ts文件中

ngOnInit(){
    this.scrollToSubscribe(sectionSubscribe);
}
scrollToSubscribe($element){
    $element.scrollIntoView({
        behavior: 'smooth',
        block: 'start',
        inline: 'nearest'
    });
}

但这样做不允许我这样做。

请帮忙。

admin 更改状态以发布 2023年5月22日
0
0 Comments

尝试像这样:

@ViewChild("sectionSubscribe", { static: true }) sectionSubscribeDiv: ElementRef;
scrollToSubscribe(){
    this.sectionSubscribeDiv.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
}

工作演示

0