Ionic framework Ionic 4在维修/防护装置中的滚动位置
我正在尝试实现一个类似于Facebook的功能,也就是说,如果你滚动了新闻提要,按下硬件后退按钮,你就会进入列表的顶部 为此,我认为Ionic framework Ionic 4在维修/防护装置中的滚动位置,ionic-framework,ionic4,Ionic Framework,Ionic4,我正在尝试实现一个类似于Facebook的功能,也就是说,如果你滚动了新闻提要,按下硬件后退按钮,你就会进入列表的顶部 为此,我认为canDeactivate的Router-Guards将是正确的方法 但是我无法找到一种方法来检查页面是否被滚动 我尝试了window.pageYOffset,但它总是返回0,访问保护中的ViewChild总是返回null 任何人都可以指导如何实现这一点吗?有两种方法可以帮助您 首先,从Ionic 4开始,您可以使用平台功能注册后退按钮处理程序: 其次,您可以使用
canDeactivate
的Router-Guards
将是正确的方法
但是我无法找到一种方法来检查页面是否被滚动
我尝试了window.pageYOffset
,但它总是返回0,访问保护中的ViewChild总是返回null
任何人都可以指导如何实现这一点吗?有两种方法可以帮助您 首先,从Ionic 4开始,您可以使用
平台
功能注册后退按钮处理程序:
其次,您可以使用Ionic 4的更多功能,称为scrollEvents
我已在其他答案中解释了如何使用此功能:
离子内容上的滚动事件:
<ion-header>
<ion-toolbar>
<ion-title>
Ion Content Scroll
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [scrollEvents]="true">
<!-- your content in here -->
</ion-content>
你需要明确你的标签,因为不同的爱奥尼亚版本做的事情非常不同。我已经根据你的题目“ionic4”把它清理干净了。
<ion-header>
<ion-toolbar>
<ion-title>
Ion Content Scroll
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [scrollEvents]="true">
<!-- your content in here -->
</ion-content>
import { Component, ViewChild } from '@angular/core';
import { Platform, IonContent } from '@ionic/angular';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
// This property will save the callback which we can unsubscribe when we leave this view
public unsubscribeBackEvent: any;
@ViewChild(IonContent) content: IonContent;
constructor(
private platform: Platform
) { }
//Called when view is loaded as ionViewDidLoad() removed from Ionic v4
ngOnInit(){
this.initializeBackButtonCustomHandler();
}
//Called when view is left
ionViewWillLeave() {
// Unregister the custom back button action for this page
this.unsubscribeBackEvent && this.unsubscribeBackEvent();
}
initializeBackButtonCustomHandler(): void {
this.unsubscribeBackEvent = this.platform.backButton.subscribeWithPriority(999999, () => {
this.content.scrollToPoint(0,0,1500);
});
/* here priority 101 will be greater then 100
if we have registerBackButtonAction in app.component.ts */
}
}