微信小程序瀑布流布局

html 复制代码
<template>
	<view id="max_box">
		<view  class="page-main">
		  <view class="card">
		      <view class="coloum1" >
		        <view class="card-item" v-for="(item,index) in cardList1" :key="item.id" :style="[{background: item.color}]">
		            <image class="itemImg" :src="item.f_image" mode="widthFix"></image>
					<view class="item_text" >{{item.f_id}}</view>
		        </view>
		      </view>
		      <view class="coloum2">
		         <view class="card-item" v-for="(item,index) in cardList2" :key="item.id" :style="[{background: item.color}]">
		            <image class="itemImg" :src="item.f_image" mode="widthFix"></image>
					<view class="item_text" >{{item.f_id}}</view>
		        </view>
		      </view>
		  </view>
		</view>
	</view>
</template>

<script>
	// 获取系统信息
	const systemInfo = wx.getSystemInfoSync();
	// 获取屏幕宽度
	const screenWidth = systemInfo.screenWidth;
	import store from '@/store/index.js'
	const myrpx = store.state.myrpx
	import http from '@/util/http.js'
	export default {
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				num: '0',
				cardList:[
				],
				leftHeight:0,//左侧列表累计高度
				rightHeight:0,//右侧列表累计高度
				commonHeigt:100, //瀑布流卡片 除图片外的固有高度
				cardList1:[],//左侧列表渲染数据
				cardList2:[],//右侧列表渲染数据
			}
		},
		async onLoad() {
			this.getPublishedforum()
			
		},
		methods: {
			init(){
				//初始化两侧累计高度、两侧表数据
				//重新通过接口获取数据
				this.leftHeight = 0
				this.rightHeight = 0
				//获取图片宽高比
				this.cardList = this.cardList.filter((item,index)=>{
					//获取图片宽高比
					uni.getImageInfo({
					  src: item.f_image,
					  success: res => {
					    // 计算宽高比
					    const ratio = res.width / res.height
						//图片真实高度= 真实宽度 / 宽高比  myrpx当前机型 px换算成rpx此处省略
						item.trueHeight = (screenWidth * myrpx * 0.48) / ratio
						if(this.leftHeight <= this.rightHeight){ //如果右侧列大于左侧列的总高度  则添加左边
							this.cardList1.push(item)
							this.leftHeight =this.leftHeight + item.trueHeight + this.commonHeigt
						}else{
							this.cardList2.push(item)
							this.rightHeight =this.rightHeight + item.trueHeight + this.commonHeigt
							
						}
					  } 
					})
					return item
				})
			},
			// 获取发布的帖子数据
			getPublishedforum() {
				http('/getPublishedpost', 'POST', 
					{ 
					  start:0,
					  count:10
					},
				)
				.then((res) => {
						this.cardList = res[1].data
						this.init()
					})
			},
		}
	}
</script>

<style lang="less">
		.page-main{
			width: 100%;
			height: 100%;
			display: flex;
			justify-content: center;
			.card{
				display: grid;
				grid-template-columns: repeat(2,1fr);
				grid-gap: 10px; /* 列之间和行之间的间隙 */
			}
			.card-item{
				width: 400rpx;
				image{
					width: 100%;
				}
				margin-bottom: 10rpx;
			}
			.item_text{
				width: 100%;
				height: 100rpx;
				background-color: skyblue;
				z-index: 999;
			}
		}
</style>
相关推荐
知识分享小能手16 小时前
uni-app 入门学习教程,从入门到精通,uni-app 企业项目实战:鲁嗑瓜子项目开发知识点(9)
前端·javascript·学习·微信小程序·小程序·uni-app·vue
知识分享小能手16 小时前
uni-app 入门学习教程,从入门到精通,uni-app中uCharts组件学习((8)
vue.js·学习·ui·微信小程序·小程序·uni-app·echarts
社会底层无业大学生18 小时前
uniapp微信小程序简单表格展示
微信小程序·小程序·uni-app·vue·1024程序员节
從南走到北18 小时前
JAVA无人自助共享系统台球室源码自助开台约球交友系统源码小程序
java·微信·微信小程序·小程序·1024程序员节
2501_915918411 天前
iOS 26 查看电池容量与健康状态 多工具组合的工程实践
android·ios·小程序·https·uni-app·iphone·webview
2501_915909061 天前
iOS 架构设计全解析 从MVC到MVVM与使用 开心上架 跨平台发布 免Mac
android·ios·小程序·https·uni-app·iphone·webview
2501_916008891 天前
Web 前端开发常用工具推荐与团队实践分享
android·前端·ios·小程序·uni-app·iphone·webview
2501_915921431 天前
“HTTPS 个人化”实战,个人站点与设备调试的部署、验证与抓包排查方法
网络协议·http·ios·小程序·https·uni-app·iphone
菜鸟una1 天前
【微信小程序 + 消息订阅 + 授权】 微信小程序实现消息订阅流程介绍,代码示例(仅前端)
前端·vue.js·微信小程序·小程序·typescript·taro·1024程序员节
韩立学长1 天前
【开题答辩实录分享】以《租房小程序的设计和实现》为例进行答辩实录分享
java·spring boot·小程序