四种方式快速实现上拉触底加载效果

 5011

在智能小程序的开发过程中,上拉加载是一种十分常见的加载效果,最近也收到了一些开发者在开发上拉加载时遇到的问题,今天的内容就为您介绍一下如果想实现上拉加载,我们需要如何去做。

以下是为大家总结的四种常见的实现方式:

使用 onReachBottom 实现

使用 scroll-view 组件实现

使用信息流模板实现上拉加载

使用 swiper 组件配合 onReachBottom 实现上拉加载

使用 onReachBottom 实现

智能小程序提供了onReachBottom,即页面上拉触底事件的处理函数。可以拿在 Page 中定义 onReachBottom 处理函数,监听该页面用户上拉触底事件,从而实现上拉加载。

为方便大家直接使用看到效果,将下述代码片段,直接导入开发者工具中运行查看即可: swanide://fragment/7e944c0c3785bbdf4437c672dd0dc8e41584413934361

工具下载链接:Windows /mac

代码解析

1、swan 文件是每个智能小程序页面的展现模板,类似于 Web 开发中的 HTML,所以我们先在 swan 文件中设置商品的展现样式:

  1. <view class="goodsList">
  2.     <block s-for="item,index in goods">
  3.         <view class="goodsItem">
  4.             <view class="goodsImage">
  5.                 <image src="{{item.img}}"></image>
  6.             </view>
  7.             <view class="goodsTitle">
  8.                 <text>{{item.title}}</text>
  9.             </view>
  10.         </view>
  11.     </block>
  12. </view>
  13. <view class="loading">努力加载中...</view>

2、在 js 文件中使用onReachBottom事件,当页面滑动到页面底部时,请求下一页展示数据,即实现上拉加载的效果。

  1. ...
  2. ...
  3. onReachBottom() {
  4.     //触底时继续请求下一页展示的数据
  5.     this.initData();
  6. }

使用 scroll-view 组件实现

利用 scroll-view 组件实现上拉加载也是一种十分常见的方法,实现步骤与使用onReachBottom事件类似。

scroll-view是百度智能小程序提供的组件,可实现试图区域的横向滚动和竖向滚动。使用它的bindscrolltolower属性,当页面滚动到底部或右边的时候,则会触发scrolltolower事件,从而实现上拉加载的效果。

为方便大家直接使用看到效果,将下述代码片段,直接导入开发者工具中运行查看即可: swanide://fragment/fccd71b098a7d3921b9958ccd9dba1071584414516291

代码解析

在 swan 文件中使用 scroll-view 组件,设置商品的展现样式。当页面滑动至底部时,触发scrolltolower事件,实现试图区域的竖向滚动。

  1. ``` 
  2. <view class="intro">
  3.     <scroll-view
  4.      class="scrollview"
  5.      scroll-y
  6.      bindscrolltolower="scrolltolower"
  7.     >
  8.         <view class="goodsList">
  9.             <view s-for="item,index in goods">
  10.                 <view class="goodsItem">
  11.                     <view class="goodsImage">
  12.                         <image src="{{item.img}}"></image>
  13.                     </view>
  14.                     <view class="goodsTitle">
  15.                         <text>{{item.title}}</text>
  16.                     </view>
  17.                 </view>
  18.             </view>
  19.         </view>
  20.         <view class="loading">努力加载中...</view>
  21.     </scroll-view>
  22. </view>
  23. ```

使用信息流模板实现上拉加载

信息流模版是百度智能小程序提供的组件,可配置上拉刷新、列表加载、上拉加载功能,适用于列表信息展示,并可放置在页面的任何部分。

与其它组件功能不同,使用信息流模板时需执行下述命令行,引入页面模板。

  1. npm i @smt-ui-template/page-feed

并在进入page-feed文件夹后,执行下述命令行安装所有模板依赖。

  1. npm i

为方便大家直接使用看到效果,将下述代码片段,直接导入开发者工具中运行查看即可: swanide://fragment/71af2b7f470b29b13f792c417fc5f03c1588757790402

代码解析

1、在 swan 文件中使用信息流模板,通过 smt-spin 组件加载更多数据。

  1. <smt-feed class="smt-feed pull-down-refresh" pull-to-refresh bind:scrolltolower="scrollToLower" text="{{text}}" style="height: 100vh">
  2.     <!-- 信息流组件作为局部滚动组件,必须在它的父级或本身指定高度 -->
  3.     <view class="goodsList">
  4.         <view s-for="item,index in goods">
  5.             <view class="goodsItem">
  6.                 <view class="goodsImage">
  7.                     <image src="{{item.img}}"></image>
  8.                 </view>
  9.                 <view class="goodsTitle">
  10.                     <text>{{item.title}}</text>
  11.                 </view>
  12.             </view>
  13.         </view>
  14.     </view>
  15.     <smt-spin status="{{status}}" bind:tap="reload"></smt-spin>
  16. </smt-feed>

2、在js文件中,利用在smt-spin组件上绑定的事件,实现加载更多的数据。

  1. ...
  2. ...
  3. async scrollToLower() {
  4.     const goods = await this.initData();
  5.     await syncSetData(this, {
  6.         goods: goods.concat(this.data.goods || [])
  7.     });
  8. },
  9. ...
  10. ...

使用 swiper 组件配合 onReachBottom 实现上拉加载

使用 swiper 组件配合 onReachBottom 的实现方法也比较常见,相较上边两种实现方式有些复杂,但同时也可以实现更加复杂的上拉加载场景。

swiper 组件是智能小程序提供的滑块视图组件,与 swiper-item 组件配合使用,可实现 swiper 组件内 swiper-item 的滑动。需要动态设置 swiper 组件的高度,来保证每次滑动到底时都能触发 onReachBottom 。

为方便大家直接使用看到效果,将下述代码片段,直接导入开发者工具中运行查看即可: swanide://fragment/20e8fd8c561418df7c4f24a850bf43461585224391100

代码解析

1、根据实际场景需要在 swan 文件中设置 tab,当设置多个tab时,实现效果如下:

  1. <view class="swiper-tab">
  2.    <view class="tab-item {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swiperNav">Tab1</view> 
  3.    <view class="tab-item {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swiperNav">Tab2</view>
  4. </view>

2、在 swan 文件中使用 swiper、swiper-item 组件。

  1. <swiper class="swiper" style="height: {{swiperH}}" current="{{currentTab}}" bindchange="swiperChange">
  2.     <swiper-item class="item">
  3.         <view class="goodsList">
  4.             <view s-for="item,index in goods">
  5.                 <view class="goodsItem">
  6.                     <view class="goodsImage">
  7.                         <image bindload="imageLoad" src="{{item.img}}"></image>
  8.                     </view>
  9.                     <view class="goodsTitle">
  10.                         <text>{{item.title}}</text>
  11.                     </view>
  12.                 </view>
  13.             </view>
  14.         </view>
  15.         <view class="loading">努力加载中...</view>
  16.     </swiper-item>
  17.     <swiper-item class="item">
  18.         <view class="goodsList">
  19.             <view s-for="item,index in goods">
  20.                 <view class="goodsItem">
  21.                     <view class="goodsImage">
  22.                         <image src="{{item.img}}"></image>
  23.                     </view>
  24.                     <view class="goodsTitle">
  25.                         <text>{{item.title}}</text>
  26.                     </view>
  27.                 </view>
  28.             </view>
  29.         </view>
  30.         <view class="loading">努力加载中...</view>
  31.     </swiper-item>
  32. </swiper>

3、在 js 文件中设置 swiper 组件的高度。

  1. // 给image添加load事件,保证图片全部加载出来再计算swiper-item的高度并赋值给swiper
  2. imageLoad() {       
  3.     let len = this.data.goods.length;
  4.     this.setData({
  5.         imgLoadNum: ++ this.data.imgLoadNum
  6.     })
  7.     if(this.data.imgLoadNum === len){
  8.         this.queryNodeInfo();
  9.     }
  10. },
  11. // 设置swiper的高度,如果不动态设置swiper的高度,当页面滑动到底部时,不会触发onReachBottom
  12. queryNodeInfo: function(){
  13.     let currentTab = this.data.currentTab;
  14.     swan.createSelectorQuery().selectAll('.item').boundingClientRect((rect) => {  
  15.         this.setData({
  16.             swiperH: rect[currentTab].height + 'px'
  17.         })
  18.     }).exec();
  19. }

4、在 js 文件中使用onReachBottom事件,当页面滑动到页面底部时,请求下一页展示数据,即实现上拉加载的效果。

  1. onReachBottom() { 
  2.     this.initData();
  3. },

总结

使用方法 1、2、3 可快速实现简单页面的上拉加载;而使用方法 4 可实现页面中存在多个 tab 的场景,比如:最新、最热列表的切换。开发者可根据实际情况选择不同的实现方法。


作者:小花 ,github链接:https://github.com/mengbodi/swan/issues/1




本文网址:https://www.zztuku.com/index.php/detail-7951.html
站长图库 - 四种方式快速实现上拉触底加载效果
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐