下拉刷新是移动端的专有名词,指的是通过手指在屏幕上的下拉滑动操作,从而重新加载页面数据的行为。
启用下拉刷新有两种方式:
在实际开发中,推荐使用第 2 种方式,为需要的页面单独开启下拉刷新的效果。
小程序----全局配置–全局下拉刷新配置
小程序----页面配置–局部下拉刷新配置
在全局或页面的 .json 配置文件中,通过 backgroundColor 和 backgroundTextStyle 来配置下拉刷新窗口的样式,其中:
在页面的 .js 文件中,通过 onPullDownRefresh() 函数即可监听当前页面的下拉刷新事件。
在页面的 wxml 中有如下的 UI 结构,点击按钮可以让 count 值自增 +1:
<text>pages/home/home.wxmltext>
<view>{{ count }}view>
<button
bindtap="countAdd"
type="primary"
>countAddbutton>
// pages/home/home.js
Page({
data: {
count: 0
},
// 点击按钮 count ++
countAdd() {
this.setData( {
count: this.data.count+1
} )
}
})
在触发页面的下拉刷新事件的时候,如果要把 count 的值重置为 0
// pages/home/home.js
Page({
data: {
count: 0
},
// 点击按钮 count ++
countAdd() {
this.setData( {
count: this.data.count+1
} )
},
onPullDownRefresh() {
this.setData( {
count: 0
} )
}
})
当处理完下拉刷新后,下拉刷新的 loading 效果会一直显示,不会主动消失,所以需要手动隐藏下拉刷新的 loading 效果(真机上不会停止刷新,需要手动停止)。此时,调用 wx.stopPullDownRefresh() 可以停止当前页面的下拉刷新。
// pages/home/home.js
Page({
data: {
count: 0
},
// 点击按钮 count ++
countAdd() {
this.setData( {
count: this.data.count+1
} )
},
onPullDownRefresh() {
this.setData( {
count: 0
} )
// 停止刷新
wx.stopPullDownRefresh()
}
})
上拉触底是移动端的专有名词,通过手指在屏幕上的上拉滑动操作,从而加载更多数据的行为。
在页面的 .js 文件中,通过 onReachBottom() 函数即可监听当前页面的上拉触底事件。
<text>pages/home/home.wxmltext>
<view
wx:for="{{20}}"
>{{ count }}view>
<button
bindtap="countAdd"
type="primary"
>countAddbutton>
// pages/home/home.js
Page({
data: {
count: 0
},
// 点击按钮 count ++
countAdd() {
this.setData( {
count: this.data.count+1
} )
},
onPullDownRefresh() {
this.setData( {
count: 0
} )
wx.stopPullDownRefresh()
},
onReachBottom() {
console.log('触发了下拉触底事件')
}
})
上拉触底距离指的是触发上拉触底事件时,滚动条距离页面底部的距离。
可以在全局或页面的 .json 配置文件中,通过 onReachBottomDistance 属性来配置上拉触底的距离。小程序默认的触底距离是 50px,在实际开发中,可以根据自己的需求修改这个默认值。
// pages/home/home.js
Page({
data: {
colorList: []
},
getColor() {
wx.request({
url: 'https://www.escook.cn/api/color',
method: 'GET',
success: ({ data: res })=>{
this.setData( {
colorList: [...this.data.colorList, ...res.data]
} )
}
})
}
})
// pages/home/home.js
Page({
data: {
colorList: []
},
getColor() {
wx.request({
url: 'https://www.escook.cn/api/color',
method: 'GET',
success: ({ data: res })=>{
this.setData( {
colorList: [...this.data.colorList, ...res.data]
} )
}
})
},
onLoad() {
this.getColor()
}
})
<text>pages/home/home.wxmltext>
<view
wx:for="{{colorList}}"
wx:key="index"
class="num-item"
style="background-color: rgba({{item}});"
>
{{item}}
view>
/* pages/home/home.wxss */
.num-item {
border: solid 1px black;
height: 50px;
margin: 5px;
}
// pages/home/home.js
Page({
data: {
colorList: []
},
getColor() {
wx.request({
url: 'https://www.escook.cn/api/color',
method: 'GET',
success: ({ data: res })=>{
this.setData( {
colorList: [...this.data.colorList, ...res.data]
} )
}
})
},
onLoad() {
this.getColor()
},
// 下拉触底
onReachBottom() {
this.getColor()
}
})
// pages/home/home.js
Page({
data: {
colorList: []
},
getColor() {
// loading
wx.showLoading({
title: '数据加载中...'
})
wx.request({
url: 'https://www.escook.cn/api/color',
method: 'GET',
success: ({ data: res })=>{
this.setData( {
colorList: [...this.data.colorList, ...res.data]
} )
},
// 请求完成
complete: ()=>{
wx.hideLoading()
}
})
},
onLoad() {
this.getColor()
},
onReachBottom() {
this.getColor()
}
})
// pages/home/home.js
Page({
data: {
colorList: [],
// 节流
isLoading: false
},
getColor() {
// loading
// 节流
if ( this.data.isLoading ) return
// 节流
this.setData( {
isLoading: true
} )
wx.showLoading({
title: '数据加载中...'
})
wx.request({
url: 'https://www.escook.cn/api/color',
method: 'GET',
success: ({ data: res })=>{
this.setData( {
colorList: [...this.data.colorList, ...res.data]
} )
},
// 请求完成
complete: ()=>{
wx.hideLoading()
// 节流
this.setData( {
isLoading: false
} )
}
})
},
onLoad() {
this.getColor()
},
onReachBottom() {
this.getColor()
}
})