Javascript 滚动顶部在角度2下工作不正常
我已经创建了一个表,其中的行是动态填充的。我有一个添加按钮,我可以添加行以及。在这里,我想在添加一行后在底部的表中显示新添加的行,所以我使用下面的代码是component.tsJavascript 滚动顶部在角度2下工作不正常,javascript,html,angular,Javascript,Html,Angular,我已经创建了一个表,其中的行是动态填充的。我有一个添加按钮,我可以添加行以及。在这里,我想在添加一行后在底部的表中显示新添加的行,所以我使用下面的代码是component.ts this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight; 但是上面的代码似乎不能正常工作问题是,当我添加新行时,scroll没有完全进入div标记的底部,因此无法看到新行。 下面
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
但是上面的代码似乎不能正常工作问题是,当我添加新行时,scroll没有完全进入div标记的底部,因此无法看到新行。
下面是我的html代码,我正在div标记中使用一个表,如下所示
<div style="height:200px;overflow-y:auto">
<table class="table table-bordered table-hover">
<thead>
<tr class="d-flex">
<th class="col-3">Q.No</th>
<th class="col-4">Record Answer</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let answer of answerPaper; let i = index" class="d-flex">
<td class="col-3">answer.no</td>
<td class="col-4">answer.name</td>
</tr>
</tbody>
</table>
</div>
<button (click)="addrow()">Add Row</button>
我也发布了滚动条的图片
当我添加新行时,滚动条并没有完全移动到div的底部,因此新添加的行不会被完全看到
我哪里出错了吗?在我的例子中,我想在添加新评论时向下滚动评论列表。
但逻辑是一样的。
我定义了一个变量
向下滚动:number代码>然后我恢复注释列表,如下所示:
@ViewChild('commentList') commentList: ElementRef;
然后,添加注释时:
this.scrollDown = this.commentList.nativeElement.scrollHeight;
最后,在模板中:
<div #commentList class="document-comments-list" [scrollTop]="scrollDown">
一旦角度元素准备就绪,调用滚动到顶部或底部
@ViewChild('scroll', { read: ElementRef }) public scroll: ElementRef<any>;
ngAfterViewChecked() {
this.scrollBottom()
}
public scrollBottom() {
this.scroll.nativeElement.scrollTop = this.scroll.nativeElement.scrollHeight;
}
@ViewChild('scroll',{read:ElementRef})公共滚动:ElementRef[Temp quick fix]添加计时器对我有效
ngAfterViewChecked() {
setTimeout(this.scrollBottom(), 500)
}
使用thisthis.document.body.scrollTop=0代码>谢谢你的回复。这是我一直在寻找的。它对我来说很有用,在我的情况下,我只需要滚动顶部,并且只能以这种方式使用本机元素。谢谢你的回复。这就是我一直在寻找的。
ngAfterViewChecked() {
setTimeout(this.scrollBottom(), 500)
}