uniapp小程序瀑布流列表and加灰层

本文详细介绍了如何使用小程序开发实现左右分栏的瀑布流布局,通过CSS调整图片显示和高度,结合JavaScript动态获取图片尺寸并分配到左右栏。关键步骤包括设置`inline-block`布局、使用uni.getImageInfo处理图片尺寸和高度计算。
摘要由CSDN通过智能技术生成
  1. 效果展示
    ( 图片来源网络)
    在这里插入图片描述
  2. 小程序网页不同,不需要自适应屏幕,所以直接分左右两栏就好了。先写好前端代码
<template>
	<view class="tekeout">
		<view class="left">
			<!-- 图片 -->
			<image class="img" src="#" mode=""></image>
			<!-- 灰层 -->
			<view class="ying"></view>
		</view>
		<view class="right">
			<image class="img" src="#" mode=""></image>
			<view class="ying"></view>
		</view>
	</view>
</template>
  1. 现在还看不到什么东西,那就先写个css样式给个背景颜色看看是什么样子的
.img{
		background-color: #ffe5e5;
	}

效果图:
在这里插入图片描述
4. 瀑布流是固定宽度的,然后分两栏,那么继续写css

.left,.right{
		display: inline-block;
		width: 49%;
		vertical-align: top;
	}
	.left{
		margin-right: 2%;
	}
	.img{
		width: 100%;
		border-radius: 8rpx;
		background-color: #ffe5e5;
	}

效果图:
在这里插入图片描述
5. 接下来创建一个存放照片数组,图片路径就自己实际的来写啦

export default{
   data(){
   	return{
   		imgArr: [
   			{
   				cover: "/static/house1.jpeg"
   			},
   			{
   				cover: "/static/house2.jpeg"
   			},
   			{
   				cover: "/static/house3.jpeg"
   			},
   			{
   				cover: "/static/house4.jpeg"
   			},
   			{
   				cover: "/static/house5.jpeg"
   			},
   			{
   				cover: "/static/house6.jpeg"
   			},
   			{
   				cover: "/static/house2.jpeg"
   			},
   			{
   				cover: "/static/house3.jpeg"
   				
   			},
   			{
   				cover: "/static/house6.jpeg"
   			}
   		]
   	}
   }
}

  1. 创建了一个数组,然后需要根据数组里面的高度讲图片放到左边和右边,所以
  • 初始化左右两栏
  • 初始化左右高度
  • 循环数组(循环当然要放在一个方法里list(),然后再onLoad里面调用)
  • 调用uni.getImageInfo函数可以的到图片的高度(单位px,所以要单位转换)
  • 左边或右边加一张图片,高度需要累加。
<script>
  export default{
  	data(){
  		return {
  			// 初始化左右栏
  			leftList: [],
  			rightList: [],
  			// 初始化左右栏高度
  			leftH: 0,
  			rightH: 0,
  			imgArr: [
  				{
  					cover: "/static/house1.jpeg"
  				},
  				{
  					cover: "/static/house2.jpeg"
  				},
  				{
  					cover: "/static/house3.jpeg"
  				},
  				{
  					cover: "/static/house4.jpeg"
  				},
  				{
  					cover: "/static/house5.jpeg"
  				},
  				{
  					cover: "/static/house6.jpeg"
  				},
  				{
  					cover: "/static/house2.jpeg"
  				},
  				{
  					cover: "/static/house3.jpeg"
  					
  				},
  				{
  					cover: "/static/house6.jpeg"
  				}
  			]
  		}
  	},
  	onLoad(){
  		this.list()
  	},
  	methods:{
  		list(){
  			const that = this
  			this.imgArr.forEach((res)=>{
  				uni.getImageInfo({
  					src: res.cover,
  					success:(image)=>{
  						
  						// 计算图片渲染高度
  						let showH = image.height * (750 / image.width);
  						// 判断左右高度
  						if(that.leftH <= that.rightH){
  							//加入图片
  							that.leftList.push(res)
  							//累加高度
  							that.leftH += showH
  						}else{
  							that.rightList.push(res)
  							that.rightH += showH
  						}
  					}
  				})
  			})
  			// 查看两边数据
  			console.log("左边:",that.leftList)
  			console.log("右边:",that.rightList)
  		}
  	}
  	
  }
</script>

然后可以在控制台看到我们左右两边的图片已经有数据了
在这里插入图片描述
7. 现在可以将数据放进去了

<template>
	<view class="tekeout">
		<view class="left" v-for="(item,index) in leftList" :key="index">
			<!-- 图片 -->
			<image class="img" :src="item.cover" mode=""></image>
			<!-- 灰层 -->
			<view class="ying"></view>
		</view>
		<view class="right" v-for="(item,index) in rightList" :key="index" >
			<image class="img" :src="item.cover" mode=""></image>
			<view class="ying"></view>
		</view>
	</view>
</template>

效果图(不是我们要的结果,按结果看应该是样式的问题):
在这里插入图片描述
然后…不能再左右栏的view里写循环,不然的话一行就不止两栏了,image的mode属性也要加上widthFix,

<template>
	<view class="tekeout">
		<view class="left" >
			<view class="content-box" v-for="(item,index) in leftList" :key="index">
				<!-- 图片 -->
				<image class="img" :src="item.cover" mode="widthFix"></image>
				<!-- 灰层 -->
				<view class="ying"></view>
			</view>
			
		</view>
		<view class="right" >
			<view class="content-box" v-for="(item,index) in rightList" :key="index" >
				<image  class="img" :src="item.cover" mode="widthFix"></image>
				<view class="ying"></view>
			</view>
		</view>
	</view>
</template>

效果展示
在这里插入图片描述

  1. 最后给图片加上一层灰层就简单了,这里不透明度是0.5看着明显一点
.content-box{
		position: relative;
	}
.ying{
	position: absolute;
	top: 0;
	bottom: 4rpx;
	left: 0;
	right: 0;
	background: rgba(0, 0, 0, 0.5);
	filter: alpha(opacity=60);
	border-radius: 8rpx;
} 

在这里插入图片描述

大功告成!!!
附上全部代码

<template>
	<view class="tekeout">
		<view class="left" >
			<view class="content-box" v-for="(item,index) in leftList" :key="index">
				<!-- 图片 -->
				<image class="img" :src="item.cover" mode="widthFix"></image>
				<!-- 灰层 -->
				<view class="ying"></view>
			</view>
			
		</view>
		<view class="right" >
			<view class="content-box" v-for="(item,index) in rightList" :key="index" >
				<image  class="img" :src="item.cover" mode="widthFix"></image>
				<view class="ying"></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				// 初始化左右栏
				leftList: [],
				rightList: [],
				// 初始化左右栏高度
				leftH: 0,
				rightH: 0,
				imgArr: [
					{
						cover: "/static/house1.jpeg"
					},
					{
						cover: "/static/house2.jpeg"
					},
					{
						cover: "/static/house3.jpeg"
					},
					{
						cover: "/static/house4.jpeg"
					},
					{
						cover: "/static/house5.jpeg"
					},
					{
						cover: "/static/house6.jpeg"
					},
					{
						cover: "/static/house2.jpeg"
					},
					{
						cover: "/static/house3.jpeg"
						
					},
					{
						cover: "/static/house6.jpeg"
					}
				]
			}
		},
		onLoad(){
			this.list()
		},
		methods:{
			list(){
				const that = this
				let inde = 0;
				this.imgArr.forEach((res)=>{
					
					uni.getImageInfo({
						src: res.cover,
						success:(image)=>{
							
							// 计算图片渲染高度
							let showH = image.height * (750 / image.width);
							// 判断左右盒子高度
							if(that.leftH <= that.rightH){
								that.leftList.push(res)
								that.leftH += showH
							}else{
								that.rightList.push(res)
								that.rightH += showH
							}
						}
					})
				})
				
				// console.log("左边:",that.leftList)
				// console.log("右边:",that.rightList)
			}
		}
		
	}
</script>

<style scoped>
	.tekeout {
		padding: 0 15rpx;
		font-size: 14rpx;
		line-height: 24rpx;
	}
	.content-box{
		position: relative;
	}
	.left,.right{
		display: inline-block;
		width: 49%;
		vertical-align: top;
	}
	.left{
		margin-right: 2%;
	}
	.img{
		width: 100%;
		border-radius: 8rpx;
		/* background-color: #ffe5e5; */
	}
	.ying{
		position: absolute;
		top: 0;
		bottom: 4rpx;
		left: 0;
		right: 0;
		background: rgba(0, 0, 0, 0.5);
		filter: alpha(opacity=60);
		border-radius: 8rpx;
	} 
	
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值