- 效果展示
( 图片来源网络)
- 小程序网页不同,不需要自适应屏幕,所以直接分左右两栏就好了。先写好前端代码
<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>
- 现在还看不到什么东西,那就先写个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"
}
]
}
}
}
- 创建了一个数组,然后需要根据数组里面的高度讲图片放到左边和右边,所以
- 初始化左右两栏
- 初始化左右高度
- 循环数组(循环当然要放在一个方法里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>
效果展示
- 最后给图片加上一层灰层就简单了,这里不透明度是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>