小程序的下拉刷新非常容易(写惯了android,使用小程序实在是太爽了),但是还是有些细节需要注意。
1. 开启下拉刷新
在xx.json 中将enablePullDownRefresh设为true
{ "navigationBarTitleText": "下拉刷新", "enablePullDownRefresh": true }
或者,在全局的app.json中开启所有页面的下拉刷新
"window":{ "backgroundTextStyle":"dark", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle":"black", "enablePullDownRefresh":true }
2. 在 Page 中定义 onPullDownRefresh 处理函数,监听该页面用户下拉刷新事件
Page({ data: { }, onPullDownRefresh: function () { console.log("下拉刷新"); //这里写业务逻辑代码,比如,向服务器获取最新数据 } })
3. 手动关闭下拉刷新,隐藏loading,让页面回到顶端
调用api的wx.stopPullDownRefresh()
//index.js //获取应用实例 var app = getApp() Page({ data: { }, onPullDownRefresh: function () { console.log("下拉刷新"); }, //“停止刷新”按钮点击事件 stopPullDownRefresh: function (e) { wx.stopPullDownRefresh({ complete: function (res) { console.log("停止刷新") } }) } })
注意:如果下拉后的背景是白色的,你需要将app.json 中window的backgroundTextStyle设为dark, 否则无法显示loading动画