【VUE】vue3 SFC方式实现九宫格效果

2023-11-11 08:11

本文主要是介绍【VUE】vue3 SFC方式实现九宫格效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果图

九宫格效果

调用方式

<template><grid class="grid-demo" isScale><grid-item class="grid-demo-item">1</bg-grid-item><grid-item class="grid-demo-item">2</bg-grid-item><grid-item class="grid-demo-item">3</bg-grid-item><grid-item class="grid-demo-item">4</bg-grid-item><grid-item class="grid-demo-item">5</bg-grid-item><grid-item class="grid-demo-item">6</bg-grid-item><grid-item class="grid-demo-item">7</bg-grid-item><grid-item class="grid-demo-item">8</bg-grid-item><grid-item class="grid-demo-item">9</bg-grid-item></grid>
<template><style scoped>
.grid-demo{background-color: #2b2d42;
}
.grid-demo-item{color: #fff;background-color: #41497D;
}
</style>

grid-item组件代码

<script setup>
import {ref, inject, watch} from 'vue'defineOptions({name: 'GridItem'
})
const isScale = inject("isScale");// 自适应高度等于宽度
const liDom = ref();
const height = ref("");
watch(() => liDom.value, (dom) => {if (isScale) {height.value = dom.clientWidth + "px";} else {height.value = "";}
}, {deep: true,
});</script><template><li class="grid-layout-item" ref="liDom"><slot /></li>
</template><style scoped lang="scss">
.grid-layout-item {float: left;list-style: none;height: v-bind(height);line-height: v-bind(height);text-align: center;vertical-align: middle;margin-bottom: 10px;
}
</style>

grid组件代码

<script setup>
import {useSlots, provide, ref, computed, watch, onMounted} from "vue";defineOptions({name: "Grid"
})
const props = defineProps({/** 间隔 */gutter: {type: Number, default: 10},/** 列数 */column: {type: Number, default: 3},/** 约束宽=高 */isScale: {type: Boolean, default: false},
})provide('isScale', props.isScale)
// 插槽列表
const slotList = useSlots()?.default() || []
// 渲染列表
const renderList = ref([])
const columnNum = ref(props.column || 1)
// 尾行编号
const lastRow = ref(0);const gridStyles = computed(() => {return {overflow: "hidden",padding: `${props.gutter}px`,margin: "0px"}
})function _SlotListHandler(el, index) {if (typeof el.type === "object" && el.type.name === "GridItem") {if (!el.props) el.props = {};if (!el.props.style) el.props.style = {};el.props.style.width = `calc((100% - ${props.gutter * (columnNum.value - 1)}px) / ${columnNum.value})`;// 右边距设置和下边距设置el.props.style.marginRight = `${props.gutter}px`;el.props.style.marginBottom = `${props.gutter}px`;// 每行最后一个不加右边距if ((index + 1) % columnNum.value === 0) el.props.style.marginRight = "0px";// 最后一行不加下边距if ((index + 1) >= lastRow.value * columnNum.value){el.props.style.marginBottom = 0;}else{el.props.style.marginBottom = `${props.gutter}px`}return el;} else if (typeof el.type === 'symbol') {let _list = [];el.children.forEach((childrenEl, childrenIndex) => {_list.push(_SlotListHandler(childrenEl, childrenIndex));})return _list;}return false;
}function renderSlot(list) {renderList.value = [];lastRow.value = Math.ceil((list[0]?.children||[]).length / columnNum.value) - 1;list.forEach((el, index) => {let _e = _SlotListHandler(el, index);if (_e instanceof Array) {renderList.value.push(..._e);} else if (typeof _e === 'object') {renderList.value.push(_e);}});
}watch(() => slotList,(list) => {console.log('list:', list)renderSlot(list);},{deep: true}
)onMounted(()=>{renderSlot(slotList);
})
</script><template><div class="grid-layout"><ul :style="gridStyles"><component v-for="(element, key) in renderList" :key="key":is="element"/></ul></div>
</template><style scoped lang="scss">
.grid-layout {list-style: none;
}
</style>

这篇关于【VUE】vue3 SFC方式实现九宫格效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/388715

相关文章

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3