在uniapp实现简易版瀑布流

文章讲述了使用Vue.js实现一个屏幕占据50%宽度的卡片布局,通过绝对和相对定位,结合JavaScript计算卡片的高度和位置,以适应不同数据的动态调整。
摘要由CSDN通过智能技术生成

1. 实现思路

设置好卡片占据屏幕的宽度的50%,使之实现两列卡片。将父盒子设置相对定位, 子元素设置为绝对定位,并且设置top 和left 为参数变量。 在onload 中获取子元素的高度,通过判断高度大小来排布卡片排列位置。

2.实现过程

2.1 遍历数据,并且设置top 和left 为参数变量,图片设置为宽度自适应。

2.2 获取所有item的高度

2.3    循环判断高度大小并给top和left赋值,并且累加该子元素所在位置的高度。

 3. 代码展示

<template>
	<view class="container">
		<view class="box">
			<view v-for="(item,index) in list " class="item" :style="{'--top2':item.top+'px','--left':item.left}">
				<image :src="item.url" mode="widthFix"></image>{{index}}
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from "@dcloudio/uni-app";
	// 图片高度的数组
	const AllData = ref([]);
	// 图片左边距离顶部的高度
	const leftHeight = ref(0);
	// 图片右边距离顶部的高度
	const rightHeight = ref(0);
	// 遍历卡片数组
	const list = ref([{
			url: "../../static/logo.png",
			left: "0",
			top: "0",
			float: "left"
		}, {
			url: "../../static/1111.png",
			left: "50%",
			top: "0",
			float: "right"
		}, {
			url: "../../static/2222.png",
			left: "0",
			top: "0",
			float: "left"
		},
		{
			url: "../../static/logo.png",
			left: "50%",
			top: "900",
		},
		{
			url: "../../static/1111.png",
			left: "50%",
			top: "900",
		},
		{
			url: "../../static/logo.png",
			left: "50%",
			top: "900",
		},
		{
			url: "../../static/logo.png",
			left: "50%",
			top: "900",
		},
		{
			url: "../../static/2222.png",
			left: "50%",
			top: "900",
		},
		{
			url: "../../static/1111.png",
			left: "50%",
			top: "900",
		},
		{
			url: "../../static/logo.png",
			left: "50%",
			top: "900",
		},
	])
	
	onLoad(() => {

		setTimeout(() => {
			// 获取卡片元素的数据
			uni.createSelectorQuery().selectAll('.item').boundingClientRect(data => {
				console.log("打印所有数据", data)
				// 截取单独高度数据
				data.forEach((item) => {
					AllData.value.push(item.height);
				})
				console.log("打印所有数据", AllData.value);	
				// 遍历高度数组
				for (let i = 0; i < AllData.value.length; i++) {
					// 判断赋值
					if (rightHeight.value < leftHeight.value ) {
						// 设置位置
						list.value[[i]].left = '50%';
						// 先进行赋值
						list.value[i].top = rightHeight.value;
						// 再进行累加
						rightHeight.value += AllData.value[i];
					} else {
						
						list.value[i].left = 0;
						list.value[i].top = leftHeight.value;
						leftHeight.value += AllData.value[i];
					}
				}

			}).exec()


		}, 1000)


	})
	
</script>

<style lang="scss" scoped>
	* {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	.container {
		width: 100%;
		height: 100%;

		.box {
			width: 100%;
			height: 100%;
			position: relative;

			.item {
				width: 50%;
				display: inline-block;
				border: 1px solid #ccc;
				box-shadow: 0 0 6px #ccc;
				break-inside: avoid;
				position: absolute;
				// 设置为变量
				top: var(--top2);
				left: var(--left);
				
				image {
					width: 100%;
				}
			}
			
		}
	}
</style>

4.实现效果

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值