【问题标题】:Angular how to scroll all the way down in a specified div from typeScriptAngular如何在typeScript的指定div中一直向下滚动
【发布时间】:2022-01-09 17:17:43
【问题描述】:

我在从 ts 滚动时遇到问题,我尝试了对其他人有用的不同方法,但都失败了

这就是我声明 div 的方式

    <div #chat id="chat" class="card-body msg_card_body">
    ...
    </div>

这是简单的 CSS

.msg_card_body {
  overflow-y: auto;
}

这就是我尝试滚动它的方式

ngAfterViewInit(): void {
    this.scrollDown();
  }

  scrollDown(){
    var container = this.el.nativeElement.querySelector("#chat");
    console.log(container.scrollTop)
    console.log(container.scrollHeight)
    container.scrollTop = container.scrollHeight;
    console.log(container.scrollTop)
  }

从各种控制台日志中,我注意到 scrollTop 为 0,scrollHeight 超过 500,并且在我将 scrollHeight 分配给 scrollTop 后,scrollTop 仍然为 0

我真的很困惑

【问题讨论】:

  • 你试过ngAfterViewChecked而不是ngAfterViewInit吗?
  • 哇,它可以工作了,非常感谢,但是你能解释一下接口之间的区别吗?

标签: html css angular typescript


【解决方案1】:

已解决,不得不调用 ngAfterViewChecked 中的函数而不是 ngAfterViewInit

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    相关资源
    最近更新 更多