angular2 滚动条到底部,发送消息框

图片描述
1、html代码

<div #scrollMe style="height: 100px; overflow: scroll; background: #fff; border: 1px solid #aaa;">
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
</div>
<button (click)="goToScrollBottom()">发送</button>

2、组件代码

import {Component} from '@angular/core';
// 滚动条使用组件
import {ElementRef, AfterViewChecked, ViewChild} from "@angular/core";

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewChecked {
    constructor() {

    }
    @ViewChild("scrollMe")
    // 获取元素
    public myScrollContainer: ElementRef;
    
    
    // 其实上面两句代码可以写成:
    // @ViewChild("scrollMe") myScrollContainer;

    ngAfterViewChecked() {
        this.scrollToBottom();
    }

    // 方法调用
    goToScrollBottom() {
        this.scrollToBottom();
    }

    scrollToBottom() {
        this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
    }
}
如何判断滚动条到底部了
if ((this.myScrollContainer.nativeElement.scrollTop + this.myScrollContainer.nativeElement.clientHeight) == this.myScrollContainer.nativeElement.scrollHeight) {
    // 如果到底不了 调用接口加载数据,追加到数组中。
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值