2016-05-15 42 views
3

我有一个离子滚动组件。 我想在滚动期间执行一些代码。在Ionic 1中,可以使用on-scroll属性并传递一个函数。Ionic 2滚动事件

该文档也丢失。在Ionic 2中是否有内置的检测滚动功能,还是我不得不使用jQuery或窗口滚动事件添加我自己的事件处理程序?

回答

2

我发现这里的解决方案:“On-scroll” not working in

使用"addScrollEventListener""ngAfterViewChecked"

+1

目前的解决方案https://forum.ionicframework.com/t/scrolling-event-in-ion-content/55563/3 – gusgard

1

您可以使用滚动部件的addScrollEventListener方法,像这样:

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

你的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> 

您的打字稿:

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

嗨,如何监听scrollStarted &scrollEnd事件离子滚动? –