-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinfinite-scroll.component.ts
44 lines (37 loc) · 1.04 KB
/
infinite-scroll.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import {
Component,
ElementRef,
EventEmitter,
OnDestroy,
OnInit,
Output,
Renderer2,
} from '@angular/core';
const ActiveZone = 150; // pixels
@Component({
selector: 'app-infinite-scroll',
template: '',
})
export class InfiniteScrollComponent implements OnInit, OnDestroy {
@Output() endReached = new EventEmitter<void>();
private _unlisten!: () => void;
private _endLock: boolean = false;
constructor(private _elRef: ElementRef, private _renderer: Renderer2) {}
ngOnInit() {
const parent = this._elRef.nativeElement.parentElement;
this._unlisten = this._renderer.listen(parent, 'scroll', (e: Event) => {
const target = e.target as HTMLElement;
const scrollTop = target.scrollTop + target.clientHeight;
const endReached = scrollTop + ActiveZone >= target.scrollHeight;
if (endReached && !this._endLock) {
this.endReached.emit();
this._endLock = true;
} else if (!endReached) {
this._endLock = false;
}
});
}
ngOnDestroy() {
this._unlisten();
}
}