如何在Ionic中滚动到页面顶部

kyks70gy  于 2022-10-24  发布在  Ionic
关注(0)|答案(8)|浏览(259)

目前我在Ionic框架上工作,我只是放了搜索框和客户的渲染列表,但假设在第一次尝试时我可以用‘a’搜索,它会显示所有有字母‘a’的项目,但问题是当我向下滚动看到搜索结果列表和底部时,如果我想用‘d’搜索,这一次它会给出结果,但在页面的顶部,但我的滚动在页面的底部。因此,为了解决上述问题,我希望在搜索查询为空时在页面顶部设置滚动位置,并显示所有客户,那么我应该做些什么来解决这个问题
在广告中感谢……

fumotvh3

fumotvh32#

对于Ionic 2和更高版本,在Content类上使用scrollToTop()方法。
Page.html

<ion-content>
  Add your content here!
</ion-content>

Page.ts

import { Component, ViewChild } from '@angular/core';
import { Content } from 'ionic-angular';

@Component({...})
export class MyPage{
  @ViewChild(Content) content: Content;

  scrollToTop() {
    this.content.scrollToTop();
  }
}

请参阅http://ionicframework.com/docs/api/components/content/Content/#scrollToTop

aor9mmx1

aor9mmx13#

我通常在我的主页上这样做。

import { Component, ViewChild } from '@angular/core';
import { Content } from 'ionic-angular';

@Component({...})
export class MyPage{
  @ViewChild(Content) content: Content;

  scrollToTop() {
    this.content.scrollToTop(400);
  }
  ionViewDidEnter(){
    this.scrollToTop();
  }
}

或者,您可以随时调用scllTop()。注意:400是以毫秒为单位的持续时间,它是可选的,您也可以使用此选项
This.content.scllToTop();
在本例中,scllToTop()将设置缺省值300。

fsi0uk1n

fsi0uk1n4#

对于Ionic 4,已将其从Content重命名为IonContent,并添加了新的程序包名称:

  • 突破:组件导入更改

使代码片段看起来如下所示:

import { Component, ViewChild } from '@angular/core';
import { IonContent } from '@ionic/angular';

@Component({...})
export class MyPage{
  @ViewChild(IonContent) content: IonContent;

  scrollToTop() {
    this.content.scrollToTop(400);
  }
  ionViewDidEnter(){
    this.scrollToTop();
  }
}

感谢@Junaid(https://stackoverflow.com/a/48987846/156388)提供这个答案的基础。
我没有编辑这个答案,因为它仍然对Ionic 2-3有效。

qhhrdooz

qhhrdooz5#

上面的答案将把你所有的观点滚动到顶部。如果你想拥有更多的控制权,那么你就必须使用委托处理程序。
首先,无论您想要滚动什么,都必须添加委托处理程序名称

<ion-content delegate-handle="dashboard">
   ......
</ion-content>

在您的控制器中,您必须使用此处理程序

$timeout(function(){$ionicScrollDelegate.$getByHandle('dashboard').scrollTop(false);});

我建议使用$Timeout,因为如果存在当前摘要周期,它将无法工作。如果要为滚动设置动画,还要将FALSE更改为TRUE。最后,别忘了在你的控制器中注入**$ionicScrollDelegate$Timeout**

ghg1uchk

ghg1uchk6#

对于Ionic 4.X最新稳定版本,请尝试
Link

在页面中,在ion-content标记上添加Angular 变量id

<ion-content
  [scrollEvents]="true"
  (ionScrollStart)="logScrollStart()"
  (ionScroll)="logScrolling($event)"
  (ionScrollEnd)="logScrollEnd()"
>

     <ion-button (click)="ScrollToTop()">
        Scroll To Top
     </ion-button>

</ion-content>

组件中添加方法

logScrollStart(){
        console.log("logScrollStart : When Scroll Starts");
      }

      logScrolling(){
        console.log("logScrolling : When Scrolling");
      }

      logScrollEnd(){
        console.log("logScrollEnd : When Scroll Ends");
      }

      ScrollToBottom(){
        this.content.scrollToBottom(1500);
      }

      ScrollToTop(){
        this.content.scrollToTop(1500);
      }

      ScrollToPoint(X,Y){
        this.content.scrollToPoint(X,Y,1500);
      }
vjhs03f7

vjhs03f77#

对于Ionic 4来说,这里的方法帮助很大。然而,第二个参数是需要的

@ViewChild(IonContent) content: IonContent;

因此,按照建议的here,我将静态设置为FALSE。但只有当我改变时,它才对我起作用:

@ViewChild(IonContent, {static: false}) content: IonContent;

@ViewChild(IonContent, undefined) content: IonContent;
smdncfj3

smdncfj38#

在Ionic 6+中,您需要声明要引用的元素的ID,您可以在声明类型时使用any,但具体来说,您可以按照以前的版本设置IonContent。这显然是因为离子的棱角分明的味道。
在添加ID之后,例如,我将我的内容命名为“Content”:

现在,您可以像以前的版本一样,通过ViewChild在打字稿中引用此元素。

现在,您可以使用文档中概述的方法。例如:

相关问题