【发布时间】:2020-07-01 11:18:02
【问题描述】:
我需要添加 MovetoTop 和 MovetoBottom 按钮。
我还需要在页面加载时自动 MovetoBottom。
调用scrollToBottom函数ngAfterViewChecked后代码不工作。(只有scrollToBottom函数工作,scrollToTop函数不工作)。
请帮忙
我在这里分享我的代码。
打字稿
import { Component, OnInit, ViewChild, ElementRef, AfterViewChecked } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-review',
templateUrl: './review.component.html',
styleUrls: ['./review.component.scss']
})
export class ReviewComponent implements OnInit {
@ViewChild('scrollMe', { read: ElementRef }) public scroll: ElementRef;
ngAfterViewChecked() {
this.scrollToBottom();
}
public scrollToBottom() {
console.log(this.scroll.nativeElement.scrollTop);
this.scroll.nativeElement.scrollTop = this.scroll.nativeElement.scrollHeight;
}
public scrollToTop() {
this.scroll.nativeElement.scrollTop = 0;
}
}
HTML
<div #scrollMe style="overflow: scroll; overflow-x: hidden;height:100%;">
Hai
</div>
【问题讨论】:
-
i need add to top, add to bottom buttons also i need automatically move to bottom when the page loading。这种说法令人困惑。 -
检查this是否适合您