我想在离子内容上有或没有滚动条时隐藏或显示元素。更具体地说,我想在没有滚动条时显示一个按钮(在列表中加载更多项目),并在有滚动条的地方隐藏它(因此更多项目的加载是由 ion-infinite-scroll 完成的)。
我的 Ionic 应用程序也将部署到桌面,因此大屏幕的用户最初不会看到滚动条,因此不会触发 ion-infinite-scroll。
这是展示该问题的演示:
主页.html
<ion-header>
<ion-toolbar>
<ion-title>
Ionic header
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<div class="ion-padding">
<p *ngFor="let item of itemList">{{ item }}</p>
<!-- How to hide this button when ion-content has a scrollbar? -->
<!-- *ngIf="???" -->
<ion-button (click)="incrementItemList(5)">Load more items</ion-button>
</div>
<ion-infinite-scroll (ionInfinite)="loadMoreItems($event)">
<ion-infinite-scroll-content loadingSpinner="crescent"></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>
<ion-footer>
<ion-toolbar>
<ion-title>
Ionic footer
</ion-title>
</ion-toolbar>
</ion-footer>
主页.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
itemList: string[] = [];
constructor() {}
ionViewWillEnter() {
this.incrementItemList(5);
}
incrementItemList(times: number) {
for (let i = 1; i <= times; i++) {
this.itemList.push(`Lorem ipsum dolor sit amet consectetur adipisicing elit. Quia placeat nam sapiente iusto eligendi`);
}
}
loadMoreItems(event: any) {
setTimeout(() => {
this.incrementItemList(15);
event.target.complete();
}, 1000);
}
}
我正在使用 Ionic 4.5.0 + Angular。
我尝试过使用获取滚动元素 https://ionicframework.com/docs/api/content#methods, 滚动高度 https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight, 客户高度 https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight, 偏移高度 https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight,但没有成功。
有任何想法吗?