Last active
September 21, 2016 02:47
-
-
Save gorango/3afd8e3680b03ecead3da4136b0b0771 to your computer and use it in GitHub Desktop.
ng2 infinite scroll directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Directive, ElementRef, EventEmitter, Input, Output} from '@angular/core'; | |
/** | |
* The infinite scroll directive. | |
* Providing a number on input offsets the distance from the bottom | |
*/ | |
@Directive({ | |
selector: '[infiniteScroll]', | |
host: { | |
'(scroll)': 'scroll($event)' | |
} | |
}) | |
export class InfiniteScroll { | |
@Output() onScroll = new EventEmitter<any>(); | |
@Input('infiniteScroll') offset: number; | |
public element: HTMLElement; | |
constructor(private _element: ElementRef) { | |
this.element = this._element.nativeElement; | |
} | |
scroll() { | |
let breakpoint = this.element.scrollTop + this.element.clientHeight + this.offset; | |
if (breakpoint >= this.element.scrollHeight) { this.onScroll.emit(null); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment