微信朋友圈图片布局

在朋友圈中,除了普遍的一行三列的布局外,一张、二张、四张图片时的布局是不一样的。一张图片时,按图片原有宽高显示。

两张图片,并行展示,图片会偏大一些。

四张图片时,一行显示两个。

三张、及四张以上时,按一行三列排序。

实现

这里结合naive ui组件实现图片展示。页面获取到 图片数据(ArrayObject)后通过 n-image-group 以组的形式展示图片。

js 复制代码
<n-scrollbar style="max-height: 60vh">
    <n-image-group>
        <div class="moment-images" :class="calssName">
            <div class="image-item" v-for="(item, index) in fileList" :key="index">
                <n-image :src="item.url" :img-props="{ style: { 'aspect-ratio': '1/1' } }" />
            </div>
        </div>
    </n-image-group>
</n-scrollbar>
<script setup>
import { ref, nextTick } from "vue";

const calssName = ref('')//类名
const fileList= ref([])//图片数据

//根据图片总长度来设置图片类名
function setClassName() {
    let length = fileList.value.length
    switch (length) {
        case 1:
            calssName.value = 'image-count-1'
            break;
        case 2:
            calssName.value = 'image-count-2'
            break;
        case 3:
            calssName.value = 'image-count-3'
            break;
        case 4:
            calssName.value = 'image-count-4'
            break;
        default:
            calssName.value = 'image-count-3'
            break;
    }
}

setClassName()
</script>

<style>
/* 单图布局 */
.img-count-1 {
  grid-template-columns: 1fr;
  max-width: 300px;
  max-height: 300px;
}

/* 双图布局 */
.image-count-2 {
  grid-template-columns: repeat(2, 1fr);
  width: 300px;
}

/* 三图-四图以上布局 */
.image-count-3 {
  grid-template-columns: repeat(3, 1fr);
  width: 350px;
}

/* 四图布局 */
.image-count-4 {
  grid-template-columns: repeat(2, 1fr);
  width: 400px;
}

.moment-images {
  display: grid;
  gap: 4px;
  margin-top: 8px;
  border-radius: 4px;
  overflow: hidden;
}

.image-item {
  aspect-ratio: 1/1;
  overflow: hidden;
  position: relative;
  background-color: #f5f5f5;
}
</style>
相关推荐
+VX:Fegn08951 分钟前
计算机毕业设计|基于java+ vue共享单车信息系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
尾善爱看海1 小时前
《JavaScript 数组操作全攻略:12 类 API + 30 个实战场景 + 20 个避坑指南》
前端·javascript·面试
Bs_MoneyMagnet1 小时前
基于springboot+vue的非遗物质文化遗产系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
计算机毕设定制辅导-无忧学长1 小时前
《基于SpringBoot的未成年人健康知识科普平台的设计与实现》
java·开发语言·vue.js·spring boot·未成年人健康知识科普平台
OpsEye1 小时前
为什么你的 Agent 莫名烧钱?聊聊循环调用的兜底方案
javascript·ai编程
linux_cfan2 小时前
16 · `custom-media-element`:属性拦截与转发
前端·javascript·音视频
右耳朵猫AI2 小时前
Web前端周刊2026W38 | React 19.3 发布、StyleX 深潜、jsdom 30.1 提速
前端·javascript·react.js·typescript·node.js
落魄大学生之流水线上谋生计2 小时前
GreenLife Carbon · OpenHarmony 智慧低碳生态平台
javascript
福兮说3 小时前
用 IndexedDB 存用户的文件,我踩过的五个坑
前端·javascript
专业程序开发源3 小时前
springboot篮球联赛管理系统13635-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·php·课程设计