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

uniapp实现图片预览,懒加载,下拉刷新等

实现的功能

  1. 懒加载 lazy-load --对小程序起效果
  2. 图片预览
  3. 下拉刷新
  4. 触底加载更多
  5. 底下设置安全距离env(safe-area-inset-bottom)
  6. 右下角固定图标置顶及刷新功能

效果如图:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
预览
在这里插入图片描述

代码

<template><view class="image-classify"><uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button" activeColor="#4cd964"></uni-segmented-control></view><view class="image-list"><view v-for="(item, index) in imageList" :key="item._id" class="image-card"><image lazy-load :src="item.url" mode="widthFix" @click="previewImage(index)"></image><view class="text">{{ item.author }}</view></view></view><view class="fixed-button-wrap"><view class="fixed-button" @click="goRefresh"><uni-icons type="refreshempty" size="30" color="#444950"></uni-icons></view><view class="fixed-button" @click="goTop"><uni-icons type="arrow-up" size="30" color="#444950"></uni-icons></view></view><!-- 加载更多 --><uni-load-more status="loading" :content-text="contentText" class="load-more" />
</template><script setup>let imageList = ref([])let current = ref(0)const classify = [ { key: 'all', name: '全部' }, { key: 'dog', name: '狗狗' }, { key: 'cat', name: '猫咪' } ]const items = computed(() => classify.map(item => item.name))const contentText = {contentrefresh: '加载中',contentnomore: '没有更多'}uni.showLoading({title: '加载中'})function getImageList() {return uni.request({url:"https://tea.qingnian8.com/tools/petShow",data: {size: 5,type: classify[current.value].key},header:{'access-key':"zhaohui6968613"},success(res) {console.log(res)imageList.value = [...imageList.value, ...res.data.data]},fail(err) {console.log('err', err)},complete() {uni.hideLoading();}})}getImageList()function onClickItem(e) {current.value = e.currentIndeximageList.value = []getImageList()}// 下拉刷新onPullDownRefresh(async() => {imageList.value = []await getImageList()uni.stopPullDownRefresh()})// 触底加载onReachBottom(() => {getImageList()})// 图片预览function previewImage(index) {const urls = imageList.value.map(item => item.url)uni.previewImage({current: index,urls})}async function goRefresh() {uni.startPullDownRefresh()imageList.value = []await getImageList()uni.stopPullDownRefresh()}function goTop() {uni.pageScrollTo({scrollTop: 0})}
</script><style lang="scss" scoped>.image-classify {padding: 50rpx;}.image-list {.image-card {margin: 50rpx;box-shadow: 0 4rpx 4rpx 10rpx #eee;.text {padding: 10rpx;text-align: right;}}}.fixed-button-wrap {position: fixed;padding-bottom: env(safe-area-inset-bottom);right: 50rpx;bottom: 0;display: flex;flex-direction: column;.fixed-button {display: flex;width: 100rpx;height: 100rpx;justify-content: center;align-items: center;margin: 10rpx;border: 1px solid #eee;border-radius: 50%;background-color: #fff;}}.load-more {height: calc(env(safe-area-inset-bottom) + 50rpx);}
</style>

说明

1.uni-ui 使用了图标,加载更多,上面tab按钮

在这里插入图片描述

2.下拉刷新不起效果记得改一下配置

在这里插入图片描述

3.https在小程序会报错

在这里插入图片描述
目前解决改一下小程序配置,如图 (后期打包时候需要配置上地址)
在这里插入图片描述

http://www.dtcms.com/a/265163.html

相关文章:

  • 基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
  • 重要版本:无需关闭UAC通知的TOS无线USB助手1.0.4,它来了(2025-07-02)
  • 操作系统考试大题-处理机调度算法-详解-1
  • 2025-暑期训练二
  • 通过具有一致性嵌入的大语言模型实现端到端乳腺癌放射治疗计划制定|文献速递-最新论文分享
  • AlpineLinux安装部署zabbix
  • 进程概念以及相关函数
  • 进程(起个开头,复习的一天)day26
  • 轻松上手:使用Nginx实现高效负载均衡
  • 应用密码学纲要
  • 怎样理解:source ~/.bash_profile
  • 决策树(Decision tree)算法详解(ID3、C4.5、CART)
  • 在线学堂-3.媒资管理模块(二)
  • 软件反调试(2)- 基于窗口列表的检测
  • 外侧三兵策略
  • 睿抗省赛2023
  • 【通识】机器学习相关
  • YOLOv11剪枝与量化(二)通道剪枝技术原理
  • 【Ragflow】30.离线环境迁移方案
  • 数据库9:数据库字符编码调整与校队(排序)规则
  • STM32F103_Bootloader程序开发11 - 实现 App 安全跳转至 Bootloader
  • UI 设计|审美积累 | 拟物化风格(Skeuomorphism)
  • 基于Jeecgboot3.8.1的vue3版本前后端分离的flowable流程管理平台
  • ai之RAG本地知识库--基于OCR和文本解析器的新一代RAG引擎:RAGFlow 认识和源码剖析
  • 学习笔记(29):训练集与测试集划分详解:train_test_split 函数深度解析
  • SimBa:实现深度强化学习参数scaling up
  • 多路I/O转接服务器(select、poll、epoll)
  • 跨境贸易的主要挑战是什么?
  • monorepo + Turborepo --- 构建仓库结构
  • 如何设置电脑定时休眠?操作指南详解