uniapp 实现下拉刷新 下滑更新

效果图

在这里插入图片描述
在app或者小程序中向下滑动 会出现刷新数据 ,而上拉到底 需要更新数据

功能实现

主要俩种方式

依赖生命周期

在page.json中开启
在这里插入图片描述
page.json

		"style" : 
			{
				"navigationBarTitleText" : "小小练习",
				"backgroundTextStyle": "dark",
				"enablePullDownRefresh": true
				}

开启后页面监听onPullDownRefresh()顶部下拉事件,onReachBottom触底事件

<template>

   <view class="content">
   	
   
					<view v-for="(item, index) in cats" :key="index">
					  <image :style="{ width: item.width/2 + 'rpx', height: item.height/2 + 'rpx' }" :src="item.url"></image>
					</view>
				
			</view>
 <view class="float">
 	<view class="item">顶部</view>
 	<view class="item">刷新</view>
 </view>
</template>

<script setup>
import { reactive,  onMounted } from 'vue';

const cats = reactive([]);
onPullDownRefresh(() => {
    console.log('触发下拉刷新了');
    // 进行下拉刷新的操作,比如重新加载数据等
    refresh(); // 这里调用你封装的刷新数据的方法
    uni.stopPullDownRefresh();
});
onReachBottom(()=>{
	console.log('触底时间')
	PullDownRefresh()
})
 const PullDownRefresh= (()=>{
	     uni.showLoading({
	     	title:'加载中',
			duration:1000
	     })
	 	    console.log('触发滑动区域刷新了')
	 	    // 停止当前页面下拉刷新
			refresh()
	 	  
	 	  
 }) 
 const addrefresh=()=>{
	 console.log('滑动到进行更新')
	uni.request({
	  url: 'https://api.thecatapi.com/v1/images/search?limit=2',
	  
	  success(res) {
	    console.log('获取接口成功', res);
	    // cats.length = 0; // 不清空清空数组
	    res.data.forEach(item => {
	      cats.push(item); // 将接口数据逐个添加到cats数组中
	    });
	  },
	  fail: (e) => {
	    console.log('获取接口失败');
	  }
	
	});
 }
const refresh=()=>{
	uni.request({
	  url: 'https://api.thecatapi.com/v1/images/search?limit=2',
	  success(res) {
	    console.log('获取接口成功', res);
	    cats.length = 0; // 不清空清空数组
	    res.data.forEach(item => {
	      cats.push(item); // 将接口数据逐个添加到cats数组中
	    });
	  },
	  fail: (e) => {
	    console.log('获取接口失败');
	  }
	});
}
onMounted(() => {
refresh()
});
</script>

<style lang="scss" scoped>
/* 样式 */
.float{
	position: absolute;
	right: 30rpx;
	bottom: 100rpx;
	.item{
		width: 90rpx;
		height: 90rpx;
		background: rgba(165, 213, 255, 0.0);
		border-radius: 50%;
		align-items: center;
		justify-content: center;
		padding-bottom: env(safe-area-inset-bottom);
		display: flex;
		border: 1px solid rebeccapurple;
		margin-bottom: 15rpx;
	}
}
</style>

依赖滚动视图组件

在这里插入图片描述
主要依赖这俩个事件
在这里插入图片描述

<template>

					<scroll-view @scrolltoupper="PullDownRefresh" scroll-y="true" style="height: 1080rpx;" class="scroll-Y" @scrolltolower="addrefresh"
						>
					<view v-for="(item, index) in cats" :key="index">
					  <image :style="{ width: item.width/2 + 'rpx', height: item.height/2 + 'rpx' }" :src="item.url"></image>
					</view>
					</scroll-view>
			
 <view class="float">
 	<view class="item">顶部</view>
 	<view class="item">刷新</view>
 </view>
</template>

<script setup>
import { reactive,  onMounted } from 'vue';

const cats = reactive([]);
 
 const PullDownRefresh= (()=>{
	     uni.showLoading({
	     	title:'加载中',
			duration:1000
	     })
	 	    console.log('触发滑动区域刷新了')
	 	    // 停止当前页面下拉刷新
			refresh()
	 	  
	 	  
 }) 
 const addrefresh=()=>{
	 console.log('滑动到进行更新')
	uni.request({
	  url: 'https://api.thecatapi.com/v1/images/search?limit=2',
	  
	  success(res) {
	    console.log('获取接口成功', res);
	    // cats.length = 0; // 不清空清空数组
	    res.data.forEach(item => {
	      cats.push(item); // 将接口数据逐个添加到cats数组中
	    });
	  },
	  fail: (e) => {
	    console.log('获取接口失败');
	  }
	
	});
 }
const refresh=()=>{
	uni.request({
	  url: 'https://api.thecatapi.com/v1/images/search?limit=2',
	  success(res) {
	    console.log('获取接口成功', res);
	    cats.length = 0; // 不清空清空数组
	    res.data.forEach(item => {
	      cats.push(item); // 将接口数据逐个添加到cats数组中
	    });
	  },
	  fail: (e) => {
	    console.log('获取接口失败');
	  }
	});
}
onMounted(() => {
refresh()
});
</script>

这种方式没有自带的动画 并且需要设置滑动区域的高度,不然可能滑倒底部没数据了但是由于没有到组件底部无法触发事件

当上拉更新数据过多时,想要在滑动顶部进行更新数据时候,就需要滑动很长事件,所以可以使用uni.pageScrollTo(OBJECT) 快速到达页面指定位置
在这里插入图片描述

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蓝胖子不是胖子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值