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

实现的功能

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

效果如图:



预览

代码

javascript 复制代码
<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.currentIndex
		imageList.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在小程序会报错

目前解决改一下小程序配置,如图 (后期打包时候需要配置上地址)

相关推荐
2501_915921431 小时前
iOS混淆工具实战 在线教育直播类 App 的课程与互动安全防护
android·安全·ios·小程序·uni-app·iphone·webview
织_网3 小时前
UniApp 页面通讯方案全解析:从 API 到状态管理的最佳实践
前端·javascript·uni-app
yuehua_zhang4 小时前
uni app 的app端 写入运行日志到指定文件夹。
前端·javascript·uni-app
2501_915106325 小时前
Charles抓包工具在接口性能优化与压力测试中的实用方法
ios·性能优化·小程序·https·uni-app·压力测试·webview
2501_9159090621 小时前
uni-app iOS 上架常见问题与解决方案,实战经验全解析
android·ios·小程序·https·uni-app·iphone·webview
编程猪猪侠1 天前
uni-app与Vue3,实现3D圆柱形旋转画廊效果
3d·uni-app
BUG创建者1 天前
uniapp vue页面传参到webview.nvue页面的html或者另一vue中
vue.js·uni-app·html
编程迪1 天前
找活招工系统源码 雇员雇主小程序 后端JAVA前端uniapp
java·spring boot·uni-app
不吃香菜的猪1 天前
uniapp中使用echarts并且支持pc端的拖动、拖拽和其他交互事件
uni-app·echarts·交互
伍哥的传说1 天前
Uni-App + Vue onLoad与onLaunch执行顺序问题完整解决方案 – 3种实用方法详解
javascript·vue.js·uni-app·事件总线·onlaunch·onload·promise状态管理