我使用角度创建了一个聊天屏幕,每次用户发送消息时,我想向下滚动到最后一条消息,此行为在chrome中工作正常,但在野生动物园中,聊天屏幕从中间开始,并且每次我发送消息时,它都出现在中间。
ngAfterViewChecked(): void {
if (this.newChatView) {
this.newChatView.nativeElement.onload = (): void => this.sendCredentialsToIframe();
}
if (this.scrollTo.bottom) this.scrollToBottom(this.scrollTo.animated);
if (this.scrollTrack.update) {
this.scrollTrack.update = false;
this.messageBlock.nativeElement.scrollTop = this.scrollTrack.scrollTop
+ (this.messageBlock.nativeElement.scrollHeight - this.scrollTrack.scrollHeight);
}
}
scrollToBottom(animated: boolean = false): void {
this.scrollTo.bottom = false;
this.scrollTo.animated = false;
if (animated) {
const interval = setInterval(() => {
const element = this.messageBlock.nativeElement;
const scrollDifference = element.scrollHeight - element.scrollTop - element.offsetHeight;
if (scrollDifference <= 0) return clearInterval(interval);
const scrollConst = 300;
const scrollBy = scrollDifference >= scrollConst ? scrollConst : scrollDifference;
element.scrollTop += scrollBy;
return 0;
}, 1);
return;
}
// console.log(this.messageBlock)
this.messageBlock.nativeElement.scrollTop = this.messageBlock.nativeElement.scrollHeight;
}
openTagModal(): void {
this.ui.dialog.tagDialog = true;
}
scrollToTop(animated: boolean = false): void {
this.scrollTo.bottom = false;
this.scrollTo.animated = false;
if (animated) {
const interval = setInterval(() => {
const element = this.messageBlock.nativeElement;
const scrollDifference = element.scrollTop;
if (scrollDifference <= 0) return clearInterval(interval);
const scrollConst = 300;
const scrollBy = scrollDifference >= scrollConst ? scrollConst : scrollDifference;
element.scrollTop -= scrollBy;
return 0;
}, 1);
return;
}
this.messageBlock.nativeElement.scrollTop = 0;
}