uniapp实现上拉加载更多及下拉刷新(假数据版)

本文档展示了如何在项目中创建并使用一个自定义的`uni-load-more`组件,该组件用于实现上拉加载更多和下拉刷新功能。详细说明了组件的结构、属性以及如何在页面中导入和配置,包括设置加载状态、文字提示和颜色等。同时,提供了完整的页面使用示例,包括数据初始化、下拉刷新和上拉加载更多事件的处理方法。
摘要由CSDN通过智能技术生成

首先在项目中跟pages同级创建一个components文件夹,在文件夹中新建一个refrtch.vue文件,将官方代码复制进去

refrtch.vue 完整文件如下:

<template>
	<view class="uni-load-more">
		<view v-show="status === 'loading' && showIcon" class="uni-load-more__img">
			<view class="load1">
				<view :style="{background:color}" />
				<view :style="{background:color}" />
				<view :style="{background:color}" />
				<view :style="{background:color}" />
			</view>
			<view class="load2">
				<view :style="{background:color}" />
				<view :style="{background:color}" />
				<view :style="{background:color}" />
				<view :style="{background:color}" />
			</view>
			<view class="load3">
				<view :style="{background:color}" />
				<view :style="{background:color}" />
				<view :style="{background:color}" />
				<view :style="{background:color}" />
			</view>
		</view>
		<text :style="{color:color}" class="uni-load-more__text">{{ status === 'more' ? contentText.contentdown : (status === 'loading' ? contentText.contentrefresh : contentText.contentnomore) }}</text>
	</view>
</template>

<script>
	export default {
		name: 'UniLoadMore',
		props: {
			status: {
				// 上拉的状态:more-loading前;loading-loading中;noMore-没有更多了
				type: String,
				default: 'more'
			},
			showIcon: {
				type: Boolean,
				default: true
			},
			color: {
				type: String,
				default: '#777777'
			},
			contentText: {
				type: Object,
				default () {
					return {
						contentdown: '上拉显示更多',
						contentrefresh: '正在加载...',
						contentnomore: '没有更多数据了'
					}
				}
			}
		},
		data() {
			return {}
		}
	}
</script>

<style>
	@charset "UTF-8";

	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80upx;
		align-items: center;
		justify-content: center
	}

	.uni-load-more__text {
		font-size: 28upx;
		color: #999
	}

	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}

	.uni-load-more__img>view {
		position: absolute
	}

	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}

	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}

	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}

	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}

	.load2 {
		transform: rotate(30deg)
	}

	.load3 {
		transform: rotate(60deg)
	}

	.load1 view:nth-child(1) {
		animation-delay: 0s
	}

	.load2 view:nth-child(1) {
		animation-delay: .13s
	}

	.load3 view:nth-child(1) {
		animation-delay: .26s
	}

	.load1 view:nth-child(2) {
		animation-delay: .39s
	}

	.load2 view:nth-child(2) {
		animation-delay: .52s
	}

	.load3 view:nth-child(2) {
		animation-delay: .65s
	}

	.load1 view:nth-child(3) {
		animation-delay: .78s
	}

	.load2 view:nth-child(3) {
		animation-delay: .91s
	}

	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}

	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}

	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}

	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}

	@-webkit-keyframes load {
		0% {
			opacity: 1
		}

		100% {
			opacity: .2
		}
	}
</style>

在页面中直接使用
内涵注释

完整的使用例子如下:

<template>
	<view>
		<view>
			<block v-for="(item , index) in list_array" :key="index">
				<view>{{ item }}</view>
			</block>
		</view>
		
		<view>
			<uni-load-more :status="status"  :content-text="contentText" color="#007aff"  />
		</view>
	</view>
</template>

<script>
	import uniLoadMore from '../../components/refetch.vue'

	export default {
		data() {
			return {
				//初始数据
				list_array: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18',
					'19',
					'20','1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'
				],
				//加载状态
				status: 'more',
				//加载状态的不同类型
				statusTypes: [{
					value: 'more',
					text: '加载前'
				}, {
					value: 'loading',
					text: '加载中'
				}, {
					value: 'noMore',
					text: '没有更多'
				}],
				//触底加载更多的提示文字
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				}
			}
		},
		//注册组件
		components: {
			uniLoadMore
		},
		onLoad: function(options) {
			setTimeout(function() {
				console.log('start pulldown');
			}, 1000);
			//开始下拉刷新,调用后触发下拉刷新动画,效果与用户手动下拉刷新一致。
			uni.startPullDownRefresh();
		},
		// 下拉刷新
		onPullDownRefresh() {
			let _this = this
			console.log('下拉刷新后只保留默认显示的几条数据');
			
			setTimeout(function() {
				//当处理完成刷新数据后,uni.stopPullDownRefresh可以停止当前页面的刷新效果
				_this.list_array=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18',
					'19',
					'20','1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'
				],
				uni.stopPullDownRefresh();
			}, 1000);
		},
		// 上拉加载
		onReachBottom() {
			let _this = this
			this.status = 'loading'
			//在当前页面显示导航条加载动画。
			uni.showNavigationBarLoading()
			
			console.log('加载更多');
			setTimeout(function() {
				for (var i = 0; i < 10; i++) {
					_this.list_array.push("000" + i)
				}
				_this.status = 'more'
				//在当前页面隐藏导航条加载动画。
				uni.hideNavigationBarLoading()
			}, 2000);
		},
		methods: {

		}
	}
</script>

<style>
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff
	}

	view {
		font-size: 28upx;
		line-height: inherit
	}

	.example {
		padding: 0 30upx 30upx
	}

	.example-title {
		font-size: 32upx;
		line-height: 32upx;
		color: #777;
		margin: 40upx 25upx;
		position: relative
	}

	.example .example-title {
		margin: 40upx 0
	}

	.example-body {
		padding: 0 40upx
	}

	uni-radio-group uni-label {
		padding: 0;
	}

	.uni-list-item__container {
		padding: 24upx 30upx;
		width: 100%;
		box-sizing: border-box;
		flex: 1;
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.uni-list-item__container:after {
		position: absolute;
		z-index: 3;
		right: 0;
		bottom: 0;
		left: 30upx;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}
</style>
1. 首先,在页面的script中定义data数据,包括list列表和page页数。 ``` data: { list: [], page: 1 } ``` 2. 在页面的onLoad生命周期中调用获取数据的方法。 ``` onLoad() { this.getData() } ``` 3. 在页面的methods方法中定义getData方法,用于获取数据。 ``` getData() { // 调用接口获取数据 api.getData(this.data.page).then(res => { // 将新数据添加到list列表中 this.setData({ list: this.data.list.concat(res.data) }) }) } ``` 4. 在页面的onReachBottom生命周期中调用加载更多的方法。 ``` onReachBottom() { this.loadMore() } ``` 5. 在页面的methods方法中定义loadMore方法,用于加载更多数据。 ``` loadMore() { // 将页数加1 this.setData({ page: this.data.page + 1 }) // 调用获取数据的方法 this.getData() } ``` 6. 在页面的onPullDownRefresh生命周期中调用下拉刷新的方法。 ``` onPullDownRefresh() { this.refresh() } ``` 7. 在页面的methods方法中定义refresh方法,用于下拉刷新数据。 ``` refresh() { // 将页数重置为1 this.setData({ page: 1 }) // 将list列表清空 this.setData({ list: [] }) // 调用获取数据的方法 this.getData() // 停止下拉刷新 wx.stopPullDownRefresh() } ``` 8. 在页面的wxml中使用scroll-view组件,并设置onScrollToLower属性为加载更多的方法,设置enablePullDownRefresh属性为true,表示开启下拉刷新。 ``` <scroll-view scroll-y="true" class="scroll-view" enable-back-to-top="{{enableBackToTop}}" enable-flex="true" onScrollToLower="loadMore" enablePullDownRefresh="{{true}}" onPullDownRefresh="refresh"> <!-- 列表内容 --> </scroll-view> ``` 9. 最后,在app.json中设置"window"字段的"enablePullDownRefresh"属性为true,表示全局开启下拉刷新。 ``` { "window": { "enablePullDownRefresh": true } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上流星&洒下星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值