ngx-admin/src/app/theme/directives/scrollPosition/scrollPosition.directive.ts

25 lines
596 B
TypeScript
Raw Normal View History

import {Directive, Input, Output, EventEmitter, HostListener} from 'angular2/core';
2016-04-29 17:27:19 +03:00
2016-04-27 18:21:52 +03:00
@Directive({
2016-04-29 17:27:19 +03:00
selector: '[scrollPosition]'
2016-04-27 18:21:52 +03:00
})
export class ScrollPosition {
2016-04-29 17:27:19 +03:00
@Input() maxHeight:Number;
@Output() scrollChange:EventEmitter<Boolean> = new EventEmitter<Boolean>();
2016-04-27 18:21:52 +03:00
2016-04-29 17:27:19 +03:00
private _isScrolled:Boolean;
2016-04-27 18:21:52 +03:00
2016-04-29 17:27:19 +03:00
ngOnInit() {
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
}
}