1

Is it possible to detect when the window is scrolled?

I have tried adding HostListener:

@HostListener("window:scroll", [])
onScroll() {
   console.log('scroll');
}

And I have tried using Renderer2:

this.renderer.listen(
   'window',
   'scroll',
   (evt) => {
      console.log('scroll');
   }
);

Neither work.

Jeremy
  • 3,620
  • 9
  • 43
  • 75

2 Answers2

2

in your html file add this

<ion-content [scrollEvents]="true"></ion-content>

in your your .ts file add this

@HostListener('ionScroll', ['$event']) onScroll(event){
    console.log(event.detail.scrollTop)
    console.log('im scrolling')
}
Gabriel Costa
  • 41
  • 1
  • 5
0

you can detect the scroll with below code, just as your first code snippet..

 @HostListener('window:scroll')
 onWindowScroll():void {
   let isScrolled = window.scrollY > this.heightToCompare;
   if (isScrolled !== false) {
      //  do what you want  
   } 
 }
Ganesh
  • 5,808
  • 2
  • 21
  • 41
  • I mentioned in my question this does not work. I have added a breakpoint and console log and this does not trigger. Maybe it's because Ionic. – Jeremy Oct 30 '19 at 05:25
  • 1
    @jrquick its not working because , you should not be setting overflow-x: hidden inside the body or should not set height:100% . ionic for some reason is auto setting height:100% – Joel Joseph Oct 30 '19 at 06:07
  • @jrquick also this may help you https://stackoverflow.com/questions/43186489/onscroll-event-ionic-2 – Joel Joseph Oct 30 '19 at 06:10