当前位置:   article > 正文

uni-app微信小程序上拉加载,下拉刷新

uni-app微信小程序上拉加载,下拉刷新

pages.json配置官网链接
onPullDownRefresh、onReachBottom函数跟生命周期同级

data() {
	return {
		orderList:[],
		total: null, //总共多少条数据
		page: 1,
		pageSize: 10,
	}
},
onLoad() {
	
},
mounted(){
	this.getInfo()
},
methods:{
	getInfo(){
		API.getListxxx().then(res => {
			const newlist = result.contents
			this.orderList.push(...newlist)
			this.total = result.totalCount
		})
	},
	//通过按钮点击触发下拉刷新
     fresh(){
          uni.startPullDownRefresh()
      }
},
//距离底部100px时(page中配置过),触发该事件页面滚动到底部的事件(不是scroll-view滚到底)
onReachBottom() {
	let allTotal = this.page * this.pageSize
	if (allTotal < this.total) {
		//当前条数小于总条数 则增加请求页数
		this.page++;
		this.getInfo() //调用加载数据方法
	} else {
		// console.log('已加载全部数据')
	}
},
//下拉后触发的事件
onPullDownRefresh() {
	this.orderList = []
	//调用获取数据方法
	this.getInfo()
	setTimeout(() => {
		//结束下拉刷新
		uni.stopPullDownRefresh();
	}, 1000);
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

onReachBottom (上拉时到底部多少距离触发的事件) 官方语言:页面滚动到底部的事件。可在pages.json里定义具体页面底部的触发距离onReachBottomDistance,比如设为50,那么滚动页面到距离底部50px时,就会触发onReachBottom事件。

//pages.json
{
	"path": "pages/index/index",
	"style": {
		"enablePullDownRefresh": true,
		"onReachBottomDistance":100
	}
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

也可以每次回到页面就调用下拉刷新(看需求定)

onShow() { //没次回到页面都会调用下拉刷新
  uni.startPullDownRefresh()
},
onLoad() { //页面最开始调用
  uni.startPullDownRefresh()
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

下拉刷新

自定义配置,在 App 平台下可以自定义部分下拉刷新的配置 page->style->app-plus->pullToRefresh。
在这里插入图片描述
代码示例

{
    "pages": [
        {
            "path": "pages/index/index", //首页
            "style": {
                "app-plus": {
                    "pullToRefresh": {
                        "support": true,
                        "color": "#ff3333",
                        "style": "default",
						"offset":"260px",
						"height":"50%",
                        "contentdown": {
                            "caption": "下拉可刷新自定义文本"
                        },
                        "contentover": {
                            "caption": "释放可刷新自定义文本"
                        },
                        "contentrefresh": {
                            "caption": "正在刷新自定义文本"
                        }
                    }
                }
            }
        }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号