rtr*_*rap 8 scroll angular ionic4 ion-content
在 Ionic v3 ion-content 中,有像“scrollTop”这样的属性。在 Ionic v4 中不再有这些属性......我如何确定用户是否到达了内容的末尾?
https://ionicframework.com/docs/v3/api/components/content/Content/ https://ionicframework.com/docs/api/content
rtp*_*rry 19
这些功能仍然可用,只是位于不同的位置。
使用 启用它后scrollEvents
,您需要使用该ionScroll
事件,然后根据getScrollElement()
函数的结果计算高度,而不是ion-content
具有固定窗口高度的 - 。
我在下面写了一个例子。您可以删除console.log
's 并将其收紧一点,我只是将它们留在里面以帮助您了解发生了什么。
示例页面:
<ion-header>
<ion-toolbar>
<ion-title>detectScrollToBottom</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [scrollEvents]="true" (ionScroll)="logScrolling($event)">
<p *ngFor="let dummy of ' '.repeat(75).split('')">Lorem ipsum dolor sit amet consectetur adipisicing elit. Quia placeat nam sapiente iusto eligendi</p>
</ion-content>
Run Code Online (Sandbox Code Playgroud)
示例代码:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-detect-scroll-to-bottom',
templateUrl: './detect-scroll-to-bottom.page.html',
styleUrls: ['./detect-scroll-to-bottom.page.scss'],
})
export class DetectScrollToBottomPage implements OnInit {
private scrollDepthTriggered = false;
constructor() { }
ngOnInit() {
}
async logScrolling($event) {
// only send the event once
if(this.scrollDepthTriggered) {
return;
}
console.log($event);
if($event.target.localName != "ion-content") {
// not sure if this is required, just playing it safe
return;
}
const scrollElement = await $event.target.getScrollElement();
console.log({scrollElement});
// minus clientHeight because trigger is scrollTop
// otherwise you hit the bottom of the page before
// the top screen can get to 80% total document height
const scrollHeight = scrollElement.scrollHeight - scrollElement.clientHeight;
console.log({scrollHeight});
const currentScrollDepth = $event.detail.scrollTop;
console.log({currentScrollDepth});
const targetPercent = 80;
let triggerDepth = ((scrollHeight / 100) * targetPercent);
console.log({triggerDepth});
if(currentScrollDepth > triggerDepth) {
console.log(`Scrolled to ${targetPercent}%`);
// this ensures that the event only triggers once
this.scrollDepthTriggered = true;
// do your analytics tracking here
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例日志: