浅谈小程序中下拉刷新和上拉加载功能怎么实现?(附代码)

 4278

本篇文章给大家介绍一下小程序中实现下拉刷新上拉加载功能的方法,希望对大家有所帮助!


浅谈小程序中下拉刷新和上拉加载功能怎么实现?(附代码)


在进行列表数据展示的时候,如果数据比较多或者更新比较快,就需要提供上拉刷新和下拉加载的功能,让提升用户的体验。

下拉刷新及上拉加载wxml文件编写

当我们使用scroll-view滑动组件展示列表时,其本身就存在下拉刷新和上拉加载的触发函数

  1. <scroll-view class="scroll" scroll-y="{{true}}" upper-threshold="50" bindscrolltoupper="refresh"  style="height:700px">
  2. <l-loadmore show="{{upfresh}}" bindscrolltolower="getMore"  type="loading" loading-text="拼命刷新中"></l-loadmore> 
  3. <l-loadmore show="{{downfresh}}" type="loading" loading-text="拼命加载中"></l-loadmore>

scroll-y: 是否允许纵向滚动,默认为false,这里我们设置为true

upper-threshold: 距顶部/左边多远时,触发 scrolltoupper 事件(下拉刷新)

bindscrolltoupper:滚动到顶部/左边时触发,这里设置滚动到顶部需要触发的函数

bindscrolltolower:滚动到顶部/右边时触发


引入line-ui框架

这里我使用的下拉刷新和上拉加载展示组件是lin-ui框架提供的,这里我说下如何引入lin-ui框架:lin-ui官方文档地址

  1. //在小程序项目目录中执行下面的函数
  2. npm install lin-ui

然后在需要引入组件的页面的json文件中进行引入

  1. "usingComponents": {
  2.     "l-loadmore":"/miniprogram_npm/lin-ui/loadmore/index",
  3.     "l-loading":"/miniprogram_npm/lin-ui/loading/index",
  4. },

这样lin-ui组件就引入成功了


js代码编写

  1. data: {
  2.     downfresh:false,//底部加载展示控制
  3.     upfresh:false//顶部加载展示控制
  4. },

首先在data中设置加载组件是否显示,默认都不显示


下拉刷新js代码

  1. //下拉刷新
  2. refresh(){
  3.     if(this.data.upfresh){
  4.         console.log("还没刷新完成")
  5.         return;
  6.     }
  7.     var that = this;
  8.     this.setData({
  9.         upfresh: true,
  10.         // upfresh:false
  11.     })
  12.     
  13.     setTimeout(function() {
  14.         //updateData为自己的数据更新逻辑代码
  15.         that.updateData(true,()=>{
  16.             that.setData({
  17.                 upfresh: false,
  18.             });
  19.         })
  20.         // wx.hideLoading();
  21.         console.info('下拉刷新加载完成.');
  22.     }, 500);
  23. },
  24.      
  25. //更新数据   
  26. updateData:function(tail, callback) {   
  27.     var that = this;
  28.     console.log("updatedata-=-=seea"+that.data.searchValue)
  29.     wx.request({
  30.         url: app.gBaseUrl + 'compony-detail/page',
  31.         method: 'GET',
  32.         data: {
  33.             page: 0,
  34.             count: 20,
  35.             componyname:that.data.searchValue
  36.         },
  37.         success: (res) => {
  38.             this.setData({
  39.                 componys: res.data
  40.             })
  41.             if (callback) {
  42.                 callback();
  43.             }
  44.         }
  45.     })
  46. },


上拉加载js代码

  1. /**
  2.  * 滑动到底部加载更多
  3.  */
  4. getMore(){
  5.     // downloadingData=this.data.downloadingData
  6.     if(this.data.downfresh){
  7.         console.log("还没加载完成")
  8.         return;
  9.     }
  10.     var that = this;
  11.     this.setData({
  12.         downfresh: true,
  13.         // upfresh:false
  14.     })
  15.    
  16.     this.setData({
  17.         downloadingData: true
  18.         // upfresh:false
  19.     })
  20.  
  21.     setTimeout(function() {
  22.         that.loadData(true,()=>{
  23.             that.setData({
  24.                 downfresh: false
  25.             });
  26.         })
  27.         // wx.hideLoading();
  28.         console.info('上拉数据加载完成.');
  29.      }, 1000);
  30.  },
  31.     
  32. loadData: function(tail, callback) {
  33.     var that = this;
  34.     wx.request({
  35.         url: app.gBaseUrl + 'compony-detail/page',
  36.         method: 'GET',
  37.         data: {
  38.             page: that.data.componys.length,
  39.             count: 20,
  40.             componyname:that.data.searchValue
  41.         },
  42.         success: (res) => {
  43.             // console.log(JSON.stringify(res.data))
  44.             that.setData({
  45.                 componys: that.data.componys.concat(res.data),
  46.             });
  47.             if (callback) {
  48.                 callback();
  49.             }
  50.         }
  51.     })
  52. },

整个下拉刷新和上拉加载的功能我们就已经实现了,主要就是利用到了scroll-view的组件特性,根据触发的时机来控制记载组件的显隐,整体实现并不是很难,具体代码可根据自己的实际情况做适当调整哦。


本文网址:https://www.zztuku.com/index.php/detail-9735.html
站长图库 - 浅谈小程序中下拉刷新和上拉加载功能怎么实现?(附代码)
申明:如有侵犯,请 联系我们 删除。

评论(0)条

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

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

    编辑推荐