【问题标题】:How to scroll to component position?如何滚动到组件位置?
【发布时间】:2021-02-17 17:41:56
【问题描述】:

我想滚动到 Angular 中的组件位置。组件是根据 repeticiones var 生成的。这是我的 HTML 代码。

这是 TS 代码:

【问题讨论】:

    标签: angular typescript scroll components


    【解决方案1】:

    我认为你可以使用 id 和 viewportScroller 来代替 elem

    HTML

    <div>
        <button (click)="onAnchorScroll(1)"></button>
        ...
    </div>
    <div *ngFor="let item of repecticiones; let i = index" [id]="i + 1">
        ...
    </div>
    

    TS

    在你的 ts 中从 @angular/common 导入 ViewportScroller

    constructor(private viewportScroller: ViewportScroller) { }
    
    onAnchorScroll(id) {
      this.viewportScroller.scrollToAnchor(id);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多