I'm trying to hide an element on my ionic app when the app is scrolled down and show when the app is scrolled up.
I am doing this through a component so I can just put tags around whatever elements I want this functionality on.
I thought I would be able to get the value of window.PageYOffset
when a scroll event fires but the window.PageYOffset
value is always 0. I thought this value would relate to the number of pixels the document is scrolled along on the Y axis, but I obviously misunderstand something.
in my hide-on-scroll
componenet I have
private eventOptions: boolean|{capture?: boolean, passive?: boolean};
ngAfterViewInit(){
//removed where I set eventOptions
this.ngZone.runOutsideAngular(() => {
window.addEventListener('scroll', this.scroll, <any>this.eventOptions);
});
}
scroll = (event: any): void => {
var a = window.pageYOffset; //value is always 0
};
this fires on every scroll movement
my HTML
<ion-header>
<hide-on-scroll>
<h1>Test</h1>
</hide-on-scroll>
</ion-header>
<ion-content class="page-background">
<div *ngIf="margins">
<div *ngFor="let val of margins.ByZone">
<div style="height:500px; margin-top:20px">
<h1>dummy content</h1>
</div>
</div>
</div>
</ion-content>
Two questions
Why is window.pageYOffset always 0?
In my scroll event how can I get a value to check if the page has being scrolled down or up?
Update
ionic is auto generating <html>
and <body>
tags and is setting a height:100%
value in them. Joel Joseph mentioned in the comments that this is a problem when trying to get window.PageYOffset
. Is there a workaround for this?