先看下效果
直接上代码
<template>
<div class="content">
<div class="box">
<div
v-for="(item, i) in imgList"
:key="i"
class="item"
:style="{width: 100 / rowCount - 1 + '%'}"
>
<img :src="item.src" />
</div>
<!-- 占位元素,如果 图片总数 % 每行数量 = 奇数 显示一个占位,如果是 偶数 显示两个占位 -->
<span
v-for="count in imgList.length % rowCount % 2 === 0 ? 2 : 1"
:key="count"
class="item"
:style="{width: 100 / rowCount - 1 + '%', height: 0}"
>{{item}}</span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
// 每行显示几张图
rowCount: 3,
imgList: [
{
id: 1,
src:'http://xxx.png'
},
{
id: 2,
src:'http://xxx.png'
},
{
id: 3,
src:'http://xxx.png'
},
{
id: 4,
src:'http://xxx.png'
},
{
id: 5,
src:'http://xxx.png'
},
{
id: 6,
src:'http://xxx.png'
}
]
};
}
};
</script>
<style lang="scss">
.content {
width: 800px;
background: rgb(0, 221, 255);
margin: 20px auto;
}
.box {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.item {
height: 300px;
margin-bottom: 10px;
img {
width: 100%;
height: 100%;
}
}
</style>