Created
February 15, 2018 21:31
-
-
Save gregor-srdic/9e22c85b4afddc45178c581caa56d048 to your computer and use it in GitHub Desktop.
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 { Content } from 'ionic-angular'; | |
import { Directive, ElementRef, Input, Renderer2, SimpleChanges } from '@angular/core'; | |
@Directive({ | |
selector: '[scrollHide]' | |
}) | |
export class ScrollHideDirective { | |
@Input('scrollHide') config: ScrollHideConfig; | |
@Input('scrollContent') scrollContent: Content; | |
contentHeight: number; | |
scrollHeight: number; | |
lastScrollPosition: number; | |
lastValue: number = 0; | |
constructor(private element: ElementRef, private renderer: Renderer2) { | |
} | |
ngOnChanges(changes: SimpleChanges) { | |
if (this.scrollContent && this.config) { | |
this.scrollContent.ionScrollStart.subscribe((ev) => { | |
this.contentHeight = this.scrollContent.getScrollElement().offsetHeight; | |
this.scrollHeight = this.scrollContent.getScrollElement().scrollHeight; | |
if (this.config.maxValue === undefined) { | |
this.config.maxValue = this.element.nativeElement.offsetHeight; | |
} | |
this.lastScrollPosition = ev.scrollTop; | |
}); | |
this.scrollContent.ionScroll.subscribe((ev) => this.adjustElementOnScroll(ev)); | |
this.scrollContent.ionScrollEnd.subscribe((ev) => this.adjustElementOnScroll(ev)); | |
} | |
} | |
private adjustElementOnScroll(ev) { | |
if (ev) { | |
ev.domWrite(() => { | |
let scrollTop: number = ev.scrollTop > 0 ? ev.scrollTop : 0; | |
let scrolldiff: number = scrollTop - this.lastScrollPosition; | |
this.lastScrollPosition = scrollTop; | |
let newValue = this.lastValue + scrolldiff; | |
newValue = Math.max(0, Math.min(newValue, this.config.maxValue)); | |
this.renderer.setStyle(this.element.nativeElement, this.config.cssProperty, `-${newValue}px`); | |
this.lastValue = newValue; | |
}); | |
} | |
} | |
} | |
export interface ScrollHideConfig { | |
cssProperty: string; | |
maxValue: number; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
@gregor-srdic
I followed your tutorial
but directive is not working in my ionic 3 project
no single error
but it’s not hide header on scroll
my home.html file is:-
My home.ts file is:-
Please help me