Skip to content

Instantly share code, notes, and snippets.

@silverio
Last active August 28, 2016 02:29
Show Gist options
  • Save silverio/1b767c6f3630cc471baebb7bd5f60ba5 to your computer and use it in GitHub Desktop.
Save silverio/1b767c6f3630cc471baebb7bd5f60ba5 to your computer and use it in GitHub Desktop.
Ionic 2 header shrink
import {ViewChild, Injectable} from "@angular/core";
import {Platform} from 'ionic-angular';
@Injectable()
export class Shrink {
scrollPosition: number = 0;
lastScrollTop: number = 0;
style: any;
height: number;
constructor(private platform: Platform) {
this.platform = platform;
if (this.platform.is('ios')) {
this.height = 44;
} else {
//android
this.height = 56;
}
}
returnStyle() {
return this.style;
}
init(content) {
content.addScrollListener((event) => {
this.scrollPosition = event.target.scrollTop;
if (this.scrollPosition > this.lastScrollTop && this.scrollPosition >= 5) {
// Scrolling Down
this.style = { 'transform': 'translateY(-' + this.height + 'px)', 'transition': 'all 0.3s linear' }
}
else {
// Scrolling up
this.style = { 'transform': 'translateY(0px)', 'transition': 'all 0.3s linear' }
}
this.lastScrollTop = this.scrollPosition;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment