uniapp实现简单图片瀑布流
由于使用uniapp开发的微信小程序不需要考虑响应式,因此瀑布流的实现相对于pc端更为简单。创建宽度一致左右两个盒子用于展示图片流,用过判断两个盒子的高度大小来决定下一张图片插入到哪个盒子中去。效果:......
·
前言:
由于使用uniapp开发的微信小程序不需要考虑响应式,因此瀑布流的实现相对于pc端更为简单。
思路:
创建宽度一致左右两个盒子用于展示图片流,用过判断两个盒子的高度大小来决定下一张图片插入到哪个盒子中去。
代码:
<!-- html -->
<template>
<view class="container">
<view class="left">
<image v-for="(item,index) in leftList" :key="index" :src="item.src" alt="" mode="widthFix"></image>
</view>
<view class="right">
<image v-for="(item,index) in rightList" :key="index" :src="item.src" alt="" mode="widthFix"></image>
</view>
</view>
</template>
export default {
data() {
return {
// 图片列表
list: [
{
src:"/static/01.JPG"
},
{
src:"/static/12.JPG"
},
{
src:"/static/03.JPG"
},
{
src:"/static/04.JPG"
},
{
src:"/static/05.PNG"
},
{
src:"/static/06.PNG"
},
{
src:"/static/08.JPG"
},
{
src:"/static/09.JPG"
},
{
src:"/static/10.jpg"
},
{
src:"/static/11.jpg"
},
{
src:"/static/02.JPG"
},
{
src:"/static/13.JPG"
}
],
// 初始化左右盒子
leftList: [],
rightList: [],
// 初始化左右盒子高度
leftH: 0,
rightH: 0
}
},
onLoad() {
this.doList()
},
methods: {
doList() {
const that = this
this.list.forEach(res => {
// 获取图片宽高
uni.getImageInfo({
src: res.src,
success: (image) => {
console.log(res.src)
// 计算图片渲染高度
let showH = (50 * image.height) / image.width
// 判断左右盒子高度
if(that.leftH <= that.rightH) {
that.leftList.push(res)
that.leftH += showH
} else {
that.rightList.push(res)
that.rightH += showH
}
}
})
})
}
}
}
.container {
padding: 0 30rpx;
font-size: 14rpx;
line-height: 24rpx;
}
.right,
.left{display: inline-block; width: 49%;vertical-align: top;}
.left{margin-right: 2%;}
.left image,
.right image{width: 100%;margin-bottom: 10rpx;}
效果:
更多推荐
所有评论(0)