Uniapp中实现加载更多、下拉刷新、返回顶部功能

一、加载更多:

在到达底部时,将新请求过来的数据追加到原来的数组即可:

复制代码
import {
	onReachBottom
} from "@dcloudio/uni-app";

const pets = ref([]); // 显示数据

function network() {
		uni.request({
			url: "https://api.thecatapi.com/v1/images/search",
			data: {
				limit: 10
			}
		}).then(res => {
			pets.value = [...pets.value, ...res.data]; 
		}).catch(error => {
			uni.showToast({
				title: '请求有误会',
				icon: "none"
			})
		}).finally(() => {
			// uni.hideLoading();
			uni.hideNavigationBarLoading();
			uni.stopPullDownRefresh();
		})
}

onReachBottom(() => {
	network();
})

二、下拉刷新:

在下拉时加载新数据:

复制代码
	import {
		onPullDownRefresh
	} from "@dcloudio/uni-app";

	onPullDownRefresh(() => {
		pets.value = []; // 先将原来的数据清空,然后加载新数据
		network();
	})

三、返回顶部:

使用Uniapp界面滚动API实现:

复制代码
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 100
		})

其它知识点:

uni.showNavigationBarLoading(); // 导航栏中显示加载状态

uni.hideNavigationBarLoading(); // 导航栏中隐藏加载状态

// 页面中显示加载状态

uni.showLoading({

title: '加载中'

});

uni.hideLoading(); // 页面中隐藏加载状态

uni.startPullDownRefresh(); // 下拉,需在pages.json对应页面的style位置开启:enablePullDownRefresh

uni.stopPullDownRefresh(); // 停止下拉

env(safe-area-inset-bottom):css中获取底部安全区高度;

组件完整代码:

复制代码
<template>
	<view class="container">
		<view class="monitor-list-wrapper">
			<view class="monitor-list" v-for="(pet, index) in pets" :key="pet.id">
				<image lazy-load :src="pet.url" mode="aspectFill" class="monitor-photo" @click="onPreview(index)">
				</image>
				<view class="monitor-title">
					{{pet.id}}
				</view>
			</view>
		</view>

		<view class="load-more">
			<uni-load-more status="loading"></uni-load-more>
		</view>

		<view class="float">
			<view class="item" @click="onRefresh"><uni-icons type="refresh" size="26" color="#888"></uni-icons></view>
			<view class="item" @click="onTop"><uni-icons type="arrow-up" size="26" color="#888"></uni-icons></view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";

	import {
		onReachBottom,
		onPullDownRefresh
	} from "@dcloudio/uni-app";

	const pets = ref([]);

	function network() {
		// uni.showLoading({
		// 	title: '加载中'
		// });
		uni.showNavigationBarLoading();

		uni.request({
			url: "https://api.thecatapi.com/v1/images/search",
			data: {
				limit: 10
			}
		}).then(res => {
			pets.value = [...pets.value, ...res.data];
		}).catch(error => {
			uni.showToast({
				title: '请求有误会',
				icon: "none"
			})
		}).finally(() => {
			// uni.hideLoading();
			uni.hideNavigationBarLoading();
			uni.stopPullDownRefresh();
		})
	}

	network();

    // 图片预览
	function onPreview(index) {
		const urls = pets.value.map(item => item.url);
		uni.previewImage({
			current: index,
			urls
		})
	}

	function onRefresh() {
		uni.startPullDownRefresh(); // 需在pages.json对应位置开启:enablePullDownRefresh
	}

	function onTop() {
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 100
		})
	}

	onReachBottom(() => {
		network();
	})

	onPullDownRefresh(() => {
		pets.value = [];
		network();
	})
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 $myuni-spacing-super-lg;
		background: #D4ECFF;
	}

	.monitor-list-wrapper {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: $myuni-spacing-lg;

		.monitor-list {
			border-radius: $myuni-border-radius-base;
			padding: $myuni-spacing-lg;
			width: 305rpx;
			background-color: $myuni-bg-color;

			.monitor-photo {
				height: 200rpx;
				width: 100%;
			}
		}

		.monitor-title {
			height: 32rpx;
			line-height: 32rpx;
			color: $myuni-text-color;
			font-size: $myuni-font-size-lg;
			text-align: center;
		}
	}

	.load-more {
		padding-bottom: calc(env(safe-area-inset-bottom) + 80rpx);
	}


	.float {
		position: fixed;
		right: 30rpx;
		bottom: 80rpx;
		padding-bottom: env(safe-area-inset-bottom);

		.item {
			width: 90rpx;
			height: 90rpx;
			border-radius: 50%;
			background-color: rgba(255, 255, 255, 0.9);
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: 1px solid #EEE;
		}
	}
</style>

四、实现效果:

相关推荐
Rysxt_3 天前
Uniapp全局配置教程
前端·uniapp
嗯嗯**4 天前
HBuilder学习1:概述、网站快速免费打包成apk
uniapp·apk·hbuilder·url快速打包成apk·网站快速打包成apk
CherishXt16 天前
对接腾讯IM,实现个人业务系统页面按钮直接跳转到和用户的聊天页面(不需要加好友)
uniapp·即时通讯·im
巴巴博一19 天前
UniApp实战:如何优雅地把 uv-ui (uv-qrcode) 生成的二维码保存到手机相册
微信小程序·uniapp·uvui
getaxiosluo20 天前
uniapp开发公众号,微信设置字体大小后,禁止改变页面字体大小
vue·uniapp·微信公众平台
特立独行的猫a21 天前
主要跨端开发框架对比:Flutter、RN、KMP、Uniapp、Cordova,谁是未来主流?
flutter·uni-app·uniapp·rn·kmp·kuikly
微:xsooop1 个月前
iOS上架被拒4.3(a) 10次到过审历程
flutter·unity·ios·uniapp
任小栗1 个月前
uniappx实现app壳子,可直接拿来用
vue·uniapp
计算机程序设计小李同学1 个月前
基于Web和Android的漫画阅读平台
java·前端·vue.js·spring boot·后端·uniapp
码界奇点1 个月前
基于Spring Boot 3与UniApp的跨平台新零售电商系统设计与实现
spring boot·uni-app·毕业设计·uniapp·零售·源代码管理