Ionic 2 scroll event

后端 未结 3 1031
梦谈多话
梦谈多话 2020-12-19 21:58

I have an ion-scroll component. I\'m trying to execute some code during scrolling. In Ionic 1 this was possible using the on-scroll attribute and passing a function.

相关标签:
3条回答
  • 2020-12-19 22:34

    You can use the addScrollEventListener method of the Scroll component, like so:

    this.scroll.addScrollEventListener((event) => {
        console.log(event);
    });
    

    Your HTML:

    <ion-header>
        <ion-navbar>
            <ion-title>Title</ion-title>
        </ion-navbar>
    </ion-header>
    
    <ion-content class="no-scroll">
        <ion-scroll></ion-scroll>
    </ion-content>
    

    Your Typescript:

    import {Component, ViewChild} from '@angular/core';
    import {Scroll} from 'ionic-angular';
    
    @Component({
        selector: 'page-home',
        templateUrl: 'home.html'
    })
    
    export class HomePage {
        @ViewChild(Scroll) scroll: Scroll;
    
        constructor() {}
    
        ngAfterViewInit() {
            this.scroll.addScrollEventListener(this.onScroll);
        }
    
        onScroll(event) {
            console.log(event);
        }
    }
    
    0 讨论(0)
  • 2020-12-19 22:34

    I found this solution from here: https://forum.ionicframework.com/t/ionic-2-rc4-addscrolllistener/73145/16?u=kamleshappster

    this.content.ionScroll.subscribe(($event) => {
     this.scrollAmount = $event.scrollTop;
    });
    
    0 讨论(0)
  • 2020-12-19 22:45

    I found a solution here: “On-scroll” not working in

    Using the "addScrollEventListener" inside "ngAfterViewChecked".

    0 讨论(0)
提交回复
热议问题