简体   繁体   English

Javascript - 自动滚动到聊天的底部

[英]Javascript - automatically scroll to the bottom of the chat

I have the following chat: 我有以下聊天:

<div class="container">
  <div class="jumbotron" id="chat" >
  </div>  

  <form (submit)="sendUserQueryToServer()">
      <div class="input-group">
          <input type="text" class="form-control" placeholder="Search" name="search" autocomplete="off" [(ngModel)]="userQuery">
          <div class="input-group-btn">
            <button class="btn btn-info"><i class="glyphicon glyphicon-search"></i></button>
          </div>
      </div>
  </form>
</div>

With the following css: 使用以下css:

#chat{
    background: lightgray;
    max-height: 60vh;
    height:60vh;
    overflow: auto;
    font-size: 1.5em;
}

and script: 和脚本:

sendUserQueryToServer() {
    $("#chat").append(this.userQuery + "<br \>" );
}

The code above is simplified, only the relevant portions are left. 上面的代码被简化,只留下相关部分。

Here is the same code, 'converted' to plain js: https://jsfiddle.net/wcukrLfk/8/ , basically does the same thing, all you have to do is press the small button (you will need to press it about 20 times to see the behavior). 这是相同的代码,“转换”为普通的js: https//jsfiddle.net/wcukrLfk/8/ ,基本上做同样的事情,所有你要做的就是按下小按钮(你需要按下它20次看行为)。

As you can see, the chat is scrollable, but when the max-height gets overflown, it doesn't automatically scroll to the bottom of the page, it just shows the scrollbar. 正如您所看到的,聊天是可滚动的,但是当最大高度溢出时,它不会自动滚动到页面底部,它只显示滚动条。 How can I make the chat auto-scroll to the bottom of the page? 如何使聊天自动滚动到页面底部?

使用此代码:

$('#chat')[0].scrollTop = $('#chat')[0].scrollHeight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM