typescript OnScroll 事件离子 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43186489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:25:13  来源:igfitidea点击:

OnScroll event Ionic 2

angulartypescriptionic-frameworkionic2

提问by BA1995

Picking up when a user scroll on Ionic 2 is confusing me. I basically want to say, when a user scrolls down the page, do something.

当用户在 Ionic 2 上滚动时拿起让我很困惑。我基本上想说,当用户向下滚动页面时,做一些事情。

Any examples would be great.

任何例子都会很棒。

UPDATE:

更新:

I have this in my constructor, so when the page scrolls I want to close the keyboard, due to it being left open and no other way to close.

我的构造函数中有这个,所以当页面滚动时我想关闭键盘,因为它保持打开状态并且没有其他方法可以关闭。

import { Component, ViewChild } from '@angular/core';
import { NavController, NavParams, Content } from 'ionic-angular';
import { Keyboard } from '@ionic-native/keyboard';

export class SearchPage {

  @ViewChild(Content)
  content:Content;

  constructor(public keyboard: Keyboard, public formBuilder: FormBuilder, public navCtrl: NavController, public navParams: NavParams, public apiAuthentication: ApiAuthentication, private http: Http) {

    this.content.ionScroll.subscribe((data)=>{
      this.keyboard.close();
    });
  }
}

However I get this error Cannot read property 'ionScroll' of undefinedam i putting it in the wrong place?

但是我收到这个错误Cannot read property 'ionScroll' of undefined是我把它放在错误的地方吗?

回答by Erevald

You can subscribe to content events.

您可以订阅内容事件。

Content has 3 output events:

内容有3 个输出事件

  • ionScrollEmitted on every scroll event.
  • ionScrollEndEmitted when scrolling ends.
  • ionScrollStartEmitted when the scrolling first starts.
  • ionScroll在每个滚动事件上发出
  • ionScrollEnd滚动结束时发出
  • ionScrollStart在滚动第一次开始时发出

Listen to an event:

监听事件:

@ViewChild(Content)
content: Content;
// ...
ngAfterViewInit() {
  this.content.ionScrollEnd.subscribe((data)=>{
    //... do things
  });
}

Or do it from the DOM:

或者从 DOM 执行:

<ion-content (ionScroll)="onScroll($event)">

For Ionic 4

对于离子 4

<ion-content [scrollEvents]="true" (ionScroll)="onScroll($event)">

回答by Mahendra Hirapra

You can use ngOnInit method to register the scroll event :

您可以使用 ngOnInit 方法注册滚动事件:

ngOnInit() {
   if (this.content) {
      this.content.ionScroll.subscribe((data)=>
        this.keyboard.close();
      });
    }
  }

回答by alex351

When inside a custom directive try with something like this:

在自定义指令中尝试使用以下内容:

import { Renderer2 } from '@angular/core';
...
constructor(private renderer: Renderer2) {}

ngOnInit() {
    this.renderer.listen(this.myElement, 'scroll', (event) => {
        // Do something with 'event'
        console.log(this.myElement.scrollTop);
    });
}