简体   繁体   English

离子 3 无限滚动在内容滚动到顶部然后底部之前不起作用

[英]ionic 3 infinite scroll is not working until content scroll to top and then bottom

I am trying to load some more data in ionic 3 project.我正在尝试在 ionic 3 项目中加载更多数据。 The infinite scroll is working only for the first time.无限滚动只在第一次工作。 And then it is stop working until the page scroll to top and then bottom.然后它停止工作,直到页面滚动到顶部然后底部。

Here is my code这是我的代码

HTML HTML

<ion-content>
    <ion-scroll style="width:100%;height:100vh" scrollY="true">
        <ion-list *ngIf="posts.length>0">
            <button *ngFor="let post of posts" ion-item>
        <ion-thumbnail item-start>
            <img [src]="getPostImage(post)">
        </ion-thumbnail>
        <h2>{{ post.title.rendered }}</h2>
    </button>
        </ion-list>
        <ion-infinite-scroll (ionInfinite)="doInfinite($event)">
            <ion-infinite-scroll-content loadingText="Loading..."></ion-infinite-scroll-content>
        </ion-infinite-scroll>
    </ion-scroll>
</ion-content>

TypeScript打字稿

doInfinite(infiniteScroll) {
    this.getCategoryPosts(infiniteScroll);
}

getCategoryPosts(infiniteScroll=null){
       this.api.getCategoryPosts({
        id : this.topic.id,
        limit : this.limit,
        page:this.page,
        success:(posts)=>{
          this.zone.run(()=>{
            if(this.page >1){
              this.posts = this.posts.concat(posts);
            }else{
              this.posts = posts;
            }
              this.page++;
              if(infiniteScroll!=null)
                  infiniteScroll.complete();
          });

        },
        error:(error)=>{
          console.log(error);
        }
      });

  }

<ion-infinite-scroll> does definitely not work well inside <ion-scroll style="width:100%;height:100vh" scrollY="true"> , at least for the following Ionic 3 versions: <ion-infinite-scroll><ion-scroll style="width:100%;height:100vh" scrollY="true"> <ion-infinite-scroll>中肯定不能很好地工作,至少对于以下 Ionic 3 版本:

$ ionic info

cli packages: (./node_modules)

    @ionic/cli-utils  : 1.19.0
    ionic (Ionic CLI) : 3.19.0

local packages:

    @ionic/app-scripts : 3.0.0
    Cordova Platforms  : android 6.2.3 ios 4.4.0
    Ionic Framework    : ionic-angular 3.9.2

Within the <ion-scroll> area of height 100vh it is required to scroll upwards to the top, before the next infinite scroll event can be triggered.height 100vh<ion-scroll>区域内,需要向上滚动到顶部,才能触发下一个无限滚动事件。 Other values for height result in the same problem. height其他值会导致相同的问题。

My best guess is, that the required height setting for the <ion-scroll> area conflicts with content size changes from the added infinite scroll items.我最好的猜测是, <ion-scroll>区域所需的高度设置与添加的无限滚动项目的内容大小发生冲突。

If you can live with a fully scrollable <ion-content> and have enough post items in your <ion-list> for reaching the infinite scroll threshold distance you can remove <ion-scroll> completely.如果您可以使用完全可滚动的<ion-content>并且您的<ion-list>有足够的帖子项以达到无限滚动阈值距离,则您可以完全删除<ion-scroll>

This was the only solution, that worked for me!这是唯一对我有用的解决方案!

I found another solution that works if you don't want a list the full height (100vh) of the screen.如果您不想列出屏幕的全高 (100vh),我找到了另一种可行的解决方案。 Set the height of the <ion-scroll> to say 70vh and then on every successful infinite scroll, when new items are added to the list, add 4vh to the height of the <ion-scroll> element.<ion-scroll>的高度设置为70vh ,然后在每次成功无限滚动时,将新项目添加到列表中时,将<ion-scroll>元素的高度添加4vh

So with a:所以有一个:

 <ion-scroll id="list-scroll" style="height: 70vh">

Then once the new items are loaded by your component:然后一旦您的组件加载了新项目:

 scrollHeight += 4; document.getElementById('list-scroll').setAttribute('style', 'height:' + scrollHeight + 'vh');

It doesn't seem to matter how tall you make the <ion-scroll>你把<ion-scroll>做成多高似乎并不重要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM