ion-infinite-scroll
Infinite Scrollコンポーネントは、ユーザーがページの下部または上部から指定された距離をスクロールしたときに実行されるアクションを呼び出します。
ユーザが定義された距離に達したときに、ionInfinite イベントに割り当てられた関数が呼び出されます。この関数がすべてのタスクを完了したら、無限スクロールインスタンスに対して
complete() メソッドを呼び出す必要があります。
Infinite Scroll Content
ion-infinite-scrollコンポーネントは、無限スクロールのロジックを持っています。コンテンツを表示するには、子コンポーネントが必要です。Ionicは、デフォルトでその
ion-infinite-scroll-content コンポーネントを使用します。このコンポーネントは、無限スクロールを表示し、無限スクロールの状態に応じて外観を変更します。ユーザが使用しているプラットフォームに応じて最適なスピナーが表示されます。ただし、ion-infinite-scroll-content コンポーネントのプロパティを設定することにより、デフォルトのスピナーを変更したり、テキストを追加することができます。
Custom Content
ion-infinite-scroll と
ion-infinite-scroll-content コンポーネントを分離することで、開発者は必要に応じて独自のコンテンツコンポーネントを作成できます。このコンテンツには、SVG要素から固有のCSSアニメーションを持つ要素まで、あらゆるものを含めることができます。
React
Infinite ScrollはReactではサポートされていません。
利用方法
<ion-content>
<ion-button (click)="toggleInfiniteScroll()" expand="block">
Toggle Infinite Scroll
</ion-button>
<ion-list></ion-list>
<ion-infinite-scroll threshold="100px" (ionInfinite)="loadData($event)">
<ion-infinite-scroll-content
loadingSpinner="bubbles"
loadingText="Loading more data...">
</ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content> import { Component, ViewChild } from '@angular/core';
import { IonInfiniteScroll } from '@ionic/angular';
@Component({
selector: 'infinite-scroll-example',
templateUrl: 'infinite-scroll-example.html',
styleUrls: ['./infinite-scroll-example.css']
})
export class InfiniteScrollExample {
@ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
constructor() {}
loadData(event) {
setTimeout(() => {
console.log('Done');
event.target.complete();
// App logic to determine if all data is loaded
// and disable the infinite scroll
if (data.length == 1000) {
event.target.disabled = true;
}
}, 500);
}
toggleInfiniteScroll() {
this.infiniteScroll.disabled = !this.infiniteScroll.disabled;
}
} <ion-content>
<ion-button onClick="toggleInfiniteScroll()" expand="block">
Toggle Infinite Scroll
</ion-button>
<ion-list></ion-list>
<ion-infinite-scroll threshold="100px" id="infinite-scroll">
<ion-infinite-scroll-content
loading-spinner="bubbles"
loading-text="Loading more data...">
</ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content> const infiniteScroll = document.getElementById('infinite-scroll');
infiniteScroll.addEventListener('ionInfinite', function(event) {
setTimeout(function() {
console.log('Done');
event.target.complete();
// App logic to determine if all data is loaded
// and disable the infinite scroll
if (data.length == 1000) {
event.target.disabled = true;
}
}, 500);
});
function toggleInfiniteScroll() {
infiniteScroll.disabled = !infiniteScroll.disabled;
} import { Component, State, h } from '@stencil/core';
@Component({
tag: 'infinite-scroll-example',
styleUrl: 'infinite-scroll-example.css'
})
export class InfiniteScrollExample {
private infiniteScroll: HTMLIonInfiniteScrollElement;
@State() data = [];
componentWillLoad() {
this.pushData();
}
pushData() {
const max = this.data.length + 20;
const min = max - 20;
for (var i = min; i < max; i++) {
this.data.push('Item ' + i);
}
// Stencil does not re-render when pushing to an array
// so create a new copy of the array
// https://stenciljs.com/jp/docs/reactive-data#handling-arrays-and-objects
this.data = [
...this.data
];
}
loadData(ev) {
setTimeout(() => {
this.pushData();
console.log('Loaded data');
ev.target.complete();
// App logic to determine if all data is loaded
// and disable the infinite scroll
if (this.data.length == 1000) {
ev.target.disabled = true;
}
}, 500);
}
toggleInfiniteScroll() {
this.infiniteScroll.disabled = !this.infiniteScroll.disabled;
}
render() {
return [
<ion-content>
<ion-button onClick={() => this.toggleInfiniteScroll()} expand="block">
Toggle Infinite Scroll
</ion-button>
<ion-list>
{this.data.map(item =>
<ion-item>
<ion-label>{item}</ion-label>
</ion-item>
)}
</ion-list>
<ion-infinite-scroll
ref={el => this.infiniteScroll = el}
onIonInfinite={(ev) => this.loadData(ev)}>
<ion-infinite-scroll-content
loadingSpinner="bubbles"
loadingText="Loading more data...">
</ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>
];
}
} プロパティ
disabled | |
|---|---|
| Description | If Set this to true to disable the infinite scroll from actively trying to receive new data while scrolling. This is useful when it is known that there is no more data that can be added, and the infinite scroll is no longer needed. |
| Attribute | disabled |
| Type | boolean |
| Default | false |
position | |
| Description | The position of the infinite scroll element.
The value can be either |
| Attribute | position |
| Type | "bottom" | "top" |
| Default | 'bottom' |
threshold | |
| Description | The threshold distance from the bottom
of the content to call the |
| Attribute | threshold |
| Type | string |
| Default | '15%' |
イベント
| Name | Description |
|---|---|
ionInfinite | Emitted when the scroll reaches the threshold distance. From within your infinite handler, you must call the infinite scroll's `complete()` method when your async operation has completed. |
メソッド
complete | |
|---|---|
| Description | Call |
| Signature | complete() => Promise<void> |

