微信小程序瀑布流布局

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>
相关推荐
從南走到北12 分钟前
JAVA国际版同城外卖跑腿团购到店跑腿多合一APP系统源码支持Android+IOS+H5
android·java·ios·微信小程序·小程序
说私域3 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的赛道力构建与品牌发展研究
人工智能·小程序
码起来呗13 小时前
基于微信小程序的旅游攻略分享互动平台设计与实现-项目分享
微信小程序·小程序·旅游
2501_9151063214 小时前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
郑叔敲代码14 小时前
帝国cms微信小程序 微信授权登录api接口
微信小程序·帝国cms小程序·帝国cms开发
dcloud_jibinbin15 小时前
【uniapp】小程序体积优化,分包异步化
前端·vue.js·webpack·性能优化·微信小程序·uni-app
2501_9160088916 小时前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
2501_9159214316 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者818 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview
00后程序员张19 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview