小程序中使用瀑布流组件的记录

一、使用场景

在使用uniapp编写的小程序中做图片标题的数据展示,采用瀑布流布局

二、业务方法

在进行业务编写过程中采取了几种方法进行

1、进行左右两列元素的动态高度进行判断,将图片数据塞入,进行高度判断,

优点:数据展示左右排列,做到动态展示不会出现某一列特别出现留白情况

缺点:数据展示过慢,会出现一张张图片跳出的情况;

我采用的是皮皮平博主写的手把手写一个uniapp小程序瀑布流组件_uniapp瀑布流-CSDN博客

可以作为参考

2、后端返回的数据中带上框高进行比例计算,或者采用uni自带的获取图片属性api进行计算

瀑布流布局深度解析(js版) - 掘金 (juejin.cn)

3、采用flex布局。直接进行左右分列展示,手动控制最大高度和溢出隐藏

因为时间问题,我实在不好意思了哈哈采用了最简单的flex布局,只要数据量够多。用户上传图片格式为竖图,就不会有太大问题

三、代码

html 复制代码
<template>
	<view class="user-ul">
		<view class="user-ul-left">
			<view class="box" @click="clickItem(item)" v-for="(item,index) in list" :key="index" v-if="index%2 == 0">
				<view class="imgBox">
					<image lazy-load="true" :src="item.indexPhotoUrl" mode="widthFix"></image>
				</view>
				<view class="text-break-all">{{ item.title }}</view>
				<view class="userMsg">
					<view class="left">
						<image class="userImg" :src="item.photoUrl" mode=""></image>
						<text class="userName">{{item.userName}}</text>
					</view>
					<view class="right" @click.stop="clickLike(item)">
						<image class="heartImg"  v-if="item.userLike==false"
							src="../../static/heart.png" mode=""></image>
						<image class="heartImg" v-else
							src="../../static/heartRed.png" mode=""></image>
						<text>{{item.likeCount}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="user-ul-right">
			<view class="box" @click="clickItem(item)" v-for="(item,index) in list" :key="index" v-if="index%2 !==0">
				<view class="imgBox">
					<image :src="item.indexPhotoUrl" mode="widthFix"></image>
				</view>
				<view class="text-break-all">{{ item.title }}</view>
				<view class="userMsg">
					<view class="left">
						<image class="userImg" :src="item.photoUrl" mode=""></image>
						<text class="userName">{{item.userName}}</text>
					</view>
					<view class="right" @click.stop="clickLike(item)">
						<image class="heartImg" v-if="item.userLike==false"
							src="../../static/heart.png" mode=""></image>
						<image class="heartImg" v-else
							src="../../static/heartRed.png" mode=""></image>
						<text>{{item.likeCount}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
javascript 复制代码
<script>
	export default {
		props: {
			list: {
				type: Array, // 列表数据
				default: []
			},
		},
		data() {
			return {
				
			}
		},
		methods:{
            //点击整个item,方便跳转详情页面
			clickItem(item){
				this.$emit('clickItem',item)
			},
            //点赞用
			clickLike(item){
				this.$emit('clickLike',item)
			}
		}
	}
</script>

我的很懒的css,

css 复制代码
.user-ul {
		box-sizing: border-box;
		padding: 22rpx 30rpx;
		padding-bottom: 110px;
		background-color: #FAFAFA;
		display: flex;

		.user-ul-left,
		.user-ul-right {
			flex: 1;
			overflow: hidden;

			.box {
				background-color: #ffffff;
				border-radius: 10rpx;
				box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.1);
				margin: 16rpx 0;

				.imgBox {
					border-top-left-radius: 10rpx;
					border-top-right-radius: 10rpx;
					max-height: 448rpx;
					overflow: hidden;

					image {
						width: 100%;
					}
				}
			}

			.text-break-all {
				font-size: 24rpx;
				font-weight: 600;
				color: #1D2129;
				line-height: 34rpx;
				word-break: break-all;
				margin: 12rpx;
			}

			.userMsg {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 0 16rpx;
				padding-bottom: 16rpx;

				.left {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 20rpx;
					font-weight: 400;
					color: #3E4452;
					line-height: 28rpx;

					.userImg {
						height: 24rpx;
						width: 24rpx;
						border-radius: 50%;
						margin-right: 8rpx;
					}

					.userName {
						font-size: 20rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #3E4452;
						line-height: 28rpx;
					}
				}

				.right {
					display: flex;
					font-size: 20rpx;
					font-weight: 400;
					color: #3E4452;
					line-height: 28rpx;
					align-items: center;

					.heartImg {
						width: 24rpx;
						height: 20rpx;
						margin-right: 4rpx;
					}

					text {
						font-size: 20rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #3E4452;
						line-height: 28rpx;
					}
				}
			}
		}

		.user-ul-left {
			box-sizing: border-box;
			padding-right: 8rpx;
		}

		.user-ul-right {
			box-sizing: border-box;
			padding-left: 8rpx;
		}
	}

如果侵权请联系我进行相关文档删除

相关推荐
Chengbei113 小时前
DSH渗透测试插件dsh-pentest全新升级!适配DeepSeek Harness,可视化探索链路,一键搭建轻量化AI渗透测试环境。
人工智能·web安全·网络安全·微信·小程序·系统安全·安全架构
2601_949950636 小时前
用练题簿小程序把课件做成可以在线刷题的内容
小程序·练习·小程序推荐
一几文9 小时前
2026年上半年软考高级-系统架构设计师考试真题回顾与解析-综合知识选择题6
小程序·架构·系统架构·软考高级·软考·it证书
zhangminghuan12 小时前
微信小程序开发核心知识点全景解析(前端必备硬核基础)
前端·微信小程序·小程序
游戏开发爱好者813 小时前
开心上架是什么,一站式 Apple 开发者工作台总览
android·小程序·https·uni-app·iphone·webview
sltin14 小时前
需求到落地:我如何在微信小程序里实现证件照换底、隐私打码与水印相机
数码相机·微信小程序·小程序
软件聚导航17 小时前
软件聚导航-前端工程师的“小家庭”,工具与生活并存
人工智能·小程序·学习方法·图片压缩
咸虾米_2 天前
小程序流量主哪种广告最赚钱?各类广告收益与实操建议
微信小程序·小程序·小程序开发·流量主广告
show4332 天前
2026小程序端AI配音技术实现:TTS多音色引擎集成与MP3生成性能优化
人工智能·性能优化·小程序
00后程序员张2 天前
Windows / Linux / Mac 上不用 Xcode 把 IPA 上传到 App Store,upload 命令详解
android·ios·小程序·https·uni-app·iphone·webview