检查用户是否在 Angular 2 中滚动到底部
Check if user has scrolled to the bottom in Angular 2
在没有 jQuery 的情况下检查用户是否在 Angular2 中滚动到页面底部的最佳做法是什么?我可以访问我的应用程序组件中的 window 吗?如果不是,我应该检查滚动到页脚组件的底部,我该怎么做?页脚组件上的指令?有人完成了吗?
// 你可以用这个。
@HostListener("window:scroll", [])
onScroll(): void {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page
}
}
对我来说,我的聊天框底部不在页面底部,所以我无法使用 window.innerHeight 查看用户是否滚动到聊天框底部。 (我的目标是始终滚动到聊天底部,除非用户试图向上滚动)
我改用了以下方法,效果很好:
let atBottom = element.scrollHeight - element.scrollTop === element.clientHeight
一些上下文:
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
disableScrollDown = false
ngAfterViewChecked() {
this.scrollToBottom();
}
private onScroll() {
let element = this.myScrollContainer.nativeElement
let atBottom = element.scrollHeight - element.scrollTop === element.clientHeight
if (this.disableScrollDown && atBottom) {
this.disableScrollDown = false
} else {
this.disableScrollDown = true
}
}
private scrollToBottom(): void {
if (this.disableScrollDown) {
return
}
try {
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
} catch(err) { }
}
和
<div class="messages-box" #scrollMe (scroll)="onScroll()">
<app-message [message]="message" *ngFor="let message of messages.slice().reverse()"></app-message>
</div>
而不是使用 document.body.offsetHeight 使用这个:
if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
// you're at the bottom of the page
}
使用@HostListener 装饰器监听 window:scroll 事件。
@HostListener('window:scroll', [])
onScroll(): void {
const triggerAt: number = 128;
/* perform an event when the user has scrolled over the point of 128px from the bottom */
if (document.body.scrollHeight - (window.innerHeight + window.scrollY) <= triggerAt) {
doSomething();
}
}
如果要将事件挂钩到可滚动的内容,请使用 scrollHeight
而不是 offsetHeight
或 clientHeight
。
在没有 jQuery 的情况下检查用户是否在 Angular2 中滚动到页面底部的最佳做法是什么?我可以访问我的应用程序组件中的 window 吗?如果不是,我应该检查滚动到页脚组件的底部,我该怎么做?页脚组件上的指令?有人完成了吗?
// 你可以用这个。
@HostListener("window:scroll", [])
onScroll(): void {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// you're at the bottom of the page
}
}
对我来说,我的聊天框底部不在页面底部,所以我无法使用 window.innerHeight 查看用户是否滚动到聊天框底部。 (我的目标是始终滚动到聊天底部,除非用户试图向上滚动)
我改用了以下方法,效果很好:
let atBottom = element.scrollHeight - element.scrollTop === element.clientHeight
一些上下文:
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
disableScrollDown = false
ngAfterViewChecked() {
this.scrollToBottom();
}
private onScroll() {
let element = this.myScrollContainer.nativeElement
let atBottom = element.scrollHeight - element.scrollTop === element.clientHeight
if (this.disableScrollDown && atBottom) {
this.disableScrollDown = false
} else {
this.disableScrollDown = true
}
}
private scrollToBottom(): void {
if (this.disableScrollDown) {
return
}
try {
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
} catch(err) { }
}
和
<div class="messages-box" #scrollMe (scroll)="onScroll()">
<app-message [message]="message" *ngFor="let message of messages.slice().reverse()"></app-message>
</div>
而不是使用 document.body.offsetHeight 使用这个:
if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
// you're at the bottom of the page
}
使用@HostListener 装饰器监听 window:scroll 事件。
@HostListener('window:scroll', [])
onScroll(): void {
const triggerAt: number = 128;
/* perform an event when the user has scrolled over the point of 128px from the bottom */
if (document.body.scrollHeight - (window.innerHeight + window.scrollY) <= triggerAt) {
doSomething();
}
}
如果要将事件挂钩到可滚动的内容,请使用 scrollHeight
而不是 offsetHeight
或 clientHeight
。