Fir*_*nks 3 html javascript angular
我的用户可以将条目添加到滚动列表的底部。但是,添加条目时滚动条不会自动向下移动,因此用户无法看到他们新添加的条目。如何让我的滚动条始终处于向下滚动的位置以显示最新条目(使用 Angular 5)?
Con*_*Fan 10
您可以通过将焦点设置在新条目上来将其滚动到视图中,如此 stackblitz 中所示。
tabindex属性,则它们可以被聚焦outline: none(以删除焦点轮廓)#commentDiv)ViewChildren由QueryList.changes事件监控HTML:
<textarea [(ngModel)]="newComment"></textarea>
<div>
<button (click)="addComment()">Add comment to list</button>
</div>
<div>
Comments
</div>
<div class="list-container">
<div tabindex="1" #commentDiv class="comment-item" *ngFor="let comment of comments">
{{ comment }}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
div.list-container {
height: 150px;
overflow: auto;
border: solid 1px black;
}
div.comment-item {
outline: none;
}
Run Code Online (Sandbox Code Playgroud)
代码:
import { Component, ViewChildren, QueryList, ElementRef, AfterViewInit } from '@angular/core';
...
export class AppComponent {
@ViewChildren("commentDiv") commentDivs: QueryList<ElementRef>;
comments = new Array<string>();
newComment: string = "Default comment content";
ngAfterViewInit() {
this.commentDivs.changes.subscribe(() => {
if (this.commentDivs && this.commentDivs.last) {
this.commentDivs.last.nativeElement.focus();
}
});
}
addComment() {
this.comments.push(this.newComment);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5471 次 |
| 最近记录: |