当前位置: 首页 > news >正文

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) 快速到达页面指定位置
在这里插入图片描述

http://www.lryc.cn/news/349403.html

相关文章:

  • 海豚调度器如何看工作流是在哪个worker节点执行
  • 凸优化理论学习三|凸优化问题(一)
  • 【Unity Shader入门精要 第7章】基础纹理补充内容:MipMap原理
  • JeeSite Vue3:前端开发页面如何动态设置菜单展示模式?
  • 微信小程序-禁止页面下拉回弹
  • 测试环境搭建整套大数据系统(十六:超级大文件处理遇到的问题)
  • C++ 并发编程指南(11)原子操作 | 11.6、计算机内存结构
  • 正则表达式教程
  • SEO之为什么研究关键词(二)
  • Mysql 创建索引
  • vaspkit 画 Charge-Density Difference
  • FreeRTOS缩小bss段的处理
  • STM32数字示波器+详细注释+上位机程序+硬件
  • 【平衡二叉树】AVL树(双旋)
  • 【保姆级介绍自动化的讲解】
  • 【大数据面试题】27 讲下Doris的物化视图
  • kylin 使用心得
  • 在线音乐系统
  • LeetCode算法题:49. 字母异位词分组(Java)
  • 第五课,输入函数、布尔类型、比较运算和if判断
  • 数学建模——线性回归模型
  • 景源畅信:抖音小店比较冷门的品类分享?
  • java项目之企业资产管理系统(springboot+vue+mysql)
  • [ardunio ide导入blinker库]
  • Llama 3 超级课堂 -笔记
  • Leetcode 第 129 场双周赛题解
  • 队列的讲解
  • 算法学习笔记(LCA)
  • 记一次苹果appstore提审拒审问题1.2
  • 在做题中学习(59):除自身以为数组的乘积