【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方式实现九宫格效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/u011159821/article/details/131891598
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/388715

相关文章

ShardingSphere之读写分离方式

《ShardingSphere之读写分离方式》:本文主要介绍ShardingSphere之读写分离方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录ShardingSphere-读写分离读写分离mysql主从集群创建 user 表主节点执行见表语句项目代码读写分

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

Python实现pdf电子发票信息提取到excel表格

《Python实现pdf电子发票信息提取到excel表格》这篇文章主要为大家详细介绍了如何使用Python实现pdf电子发票信息提取并保存到excel表格,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录应用场景详细代码步骤总结优化应用场景电子发票信息提取系统主要应用于以下场景:企业财务部门:需

基于Python实现智能天气提醒助手

《基于Python实现智能天气提醒助手》这篇文章主要来和大家分享一个实用的Python天气提醒助手开发方案,这个工具可以方便地集成到青龙面板或其他调度框架中使用,有需要的小伙伴可以参考一下... 目录项目概述核心功能技术实现1. 天气API集成2. AI建议生成3. 消息推送环境配置使用方法完整代码项目特点

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1

Spring Security介绍及配置实现代码

《SpringSecurity介绍及配置实现代码》SpringSecurity是一个功能强大的Java安全框架,它提供了全面的安全认证(Authentication)和授权(Authorizatio... 目录简介Spring Security配置配置实现代码简介Spring Security是一个功能强

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur