2016-05-11 17:38:01 +03:00
|
|
|
import {Directive, Input, Output, EventEmitter, HostListener} from '@angular/core';
|
2016-04-29 17:27:19 +03:00
|
|
|
|
2016-04-27 18:21:52 +03:00
|
|
|
@Directive({
|
2016-05-18 16:45:01 +03:00
|
|
|
selector: '[baScrollPosition]'
|
2016-04-27 18:21:52 +03:00
|
|
|
})
|
2016-05-18 16:45:01 +03:00
|
|
|
export class BaScrollPosition {
|
2016-04-27 18:21:52 +03:00
|
|
|
|
2016-05-18 16:45:01 +03:00
|
|
|
@Input() public maxHeight:number;
|
|
|
|
|
@Output() public scrollChange:EventEmitter<boolean> = new EventEmitter<boolean>();
|
2016-04-27 18:21:52 +03:00
|
|
|
|
2016-05-18 16:45:01 +03:00
|
|
|
private _isScrolled:boolean;
|
|
|
|
|
|
|
|
|
|
public ngOnInit():void {
|
2016-04-29 17:27:19 +03:00
|
|
|
this.onWindowScroll();
|
|
|
|
|
}
|
2016-04-27 18:21:52 +03:00
|
|
|
|
2016-04-29 17:27:19 +03:00
|
|
|
@HostListener('window:scroll')
|
|
|
|
|
onWindowScroll():void {
|
|
|
|
|
let isScrolled = window.scrollY > this.maxHeight;
|
|
|
|
|
if (isScrolled !== this._isScrolled) {
|
|
|
|
|
this._isScrolled = isScrolled;
|
|
|
|
|
this.scrollChange.emit(isScrolled);
|
2016-04-27 18:21:52 +03:00
|
|
|
}
|
2016-04-29 17:27:19 +03:00
|
|
|
}
|
|
|
|
|
}
|