【问题标题】:Chat type - scroll to bottom - angular 2聊天类型 - 滚动到底部 - 角度 2
【发布时间】:2017-07-02 12:14:50
【问题描述】:

在聊天中添加消息后,需要自动滚动到底部。

尝试了实现此功能的 AfterViewChecked 方法,如 [angular2 scroll to bottom (chat style) - 它工作正常,因为它使滚动移动到底部。但是当我们在聊天中添加消息后尝试向上滚动时,它不允许并且再次将视图推到聊天的底部

请就此提出解决方法。

【问题讨论】:

标签: javascript html angular


【解决方案1】:

以下代码适用于我的 Angular 4 聊天应用程序

我的component.html

<div class="feedBody"  #scrollMe (scroll)="onScroll()" >
  <div class="feedList">
  </div>
</div>

我的component.css

.feedBody {
  height: 235px;
  overflow-y: auto;
}

我的component.ts

scrollToBottom():void{
    if (this.disableScrollDown) {
      return
    }
    try {
      this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
    } catch(err) { } 
  }
onScroll(){
    let element = this.myScrollContainer.nativeElement;
    let atBottom = (element.scrollTop+200) >= (element.scrollHeight - element.offsetHeight);
    if (atBottom) {
        this.disableScrollDown = false
    } else {
        this.disableScrollDown = true
    }
  }

我使用element.scrollTop+200 是因为我想要一种行为,我不应该强制出现在屏幕底部,但可以稍微高出 200 像素。

希望它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 2016-09-21
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多