位置: IT常识 - 正文

Vue3折叠面板(Collapse)(vue叠化在哪里)

编辑:rootadmin
Vue3折叠面板(Collapse)

推荐整理分享Vue3折叠面板(Collapse)(vue叠化在哪里),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue侧边栏展开和折叠原理,vue侧边栏展开和折叠原理,vue 折叠面板,vue折叠动画效果,elementui折叠面板,vue 折叠面板,vue3折叠面板,vue折叠动画效果,内容如对您有帮助,希望把文章链接给更多的朋友!

可自定义设置以下属性:

折叠面板数据,可使用 slot 替换对应索引的 header 和 text(collapseData),必传,类型:Array<{key?: string, header?: string | slot, text?: string | slot}>,默认 []

当前激活 tab 面板的 key(activeKey),类型:number[] | number | string[] | string | null,默认 null

是否可复制面板内容(copyable),类型:number,默认 false

面板右上角固定内容,例如标识language(lang),类型:string | slot,默认 ''

面板标题和内容,字体大小(fontSize),类型:number,单位px,默认 0

Vue3折叠面板(Collapse)(vue叠化在哪里)

面板标题字体大小,优先级高于fontSize(headerFontSize),类型:number,单位px,默认 14

面板内容字体大小,优先级高于fontSize(textFontSize),类型:number,单位px,默认 14

是否展示面板上的箭头(showArrow),类型:boolean,默认 true

效果如下图:

 注:组件引用方法 import { rafTimeout } from '../index' 请参考以下博客:

使用requestAnimationFrame模拟实现setTimeout和setInterval_theMuseCatcher的博客-CSDN博客使用requestAnimationFrame模拟实现setTimeout和setInterval!https://blog.csdn.net/Dandrose/article/details/130167061

①创建折叠面板组件Collapse.vue: 

<script setup lang="ts">import { ref, watchEffect } from 'vue'// 使用 requestAnimationFrame 实现的等效 setTimeoutimport { rafTimeout } from '../index'interface Collapse { key?: string|number // 对应activeKey,如果没有传入key属性,则默认使用数据索引(0,1,2...)绑定 header?: string // 面板标题 string | slot text?: string // 面板内容 string | slot}interface Props { collapseData: Collapse[] // 折叠面板数据,可使用 slot 替换对应索引的 header 和 text activeKey?: number[] | number | string[] | string | null // 当前激活 tab 面板的 key copyable?: boolean // 是否可复制面板内容 lang?: string // 面板右上角固定内容,例如标识language string | slot fontSize?: number // 面板标题和内容,字体大小 headerFontSize?: number // 面板标题字体大小,优先级高于fontSize textFontSize?: number // 面板内容字体大小,优先级高于fontSize showArrow?: boolean // 是否展示面板上的箭头}const props = withDefaults(defineProps<Props>(), { collapseData: () => [], activeKey: null, copyable: false, lang: '', fontSize: 0, headerFontSize: 14, textFontSize: 14, showArrow: true})watchEffect(() => { const len = props.collapseData.length if (len) { getCollapseHeight(len) // 获取各个面板内容高度 }}, { flush: 'post' })const text = ref()const collapseHeight = ref<any[]>([])function getCollapseHeight (len: number) { for (let n = 0; n < len; n++) { collapseHeight.value.push(text.value[n].offsetHeight) }}const emits = defineEmits(['update:activeKey', 'change'])function dealEmit (value: any) { emits('update:activeKey', value) emits('change', value)}function onClick (key: number|string) { if (activeJudge(key)) { if (Array.isArray(props.activeKey)) { const res = (props.activeKey as any[]).filter(actKey => actKey!== key) dealEmit(res) } else { dealEmit(null) } } else { if (Array.isArray(props.activeKey)) { dealEmit([...props.activeKey, key]) } else { dealEmit(key) } }}function activeJudge (key: number|string): boolean { if (Array.isArray(props.activeKey)) { return (props.activeKey as any[]).includes(key) } else { return props.activeKey === key }}const copyTxt = ref('Copy')function onCopy (index: number) { navigator.clipboard.writeText(text.value[index].innerText || '').then(() => { /* clipboard successfully set */ copyTxt.value = 'Copied' rafTimeout(() => { copyTxt.value = 'Copy' }, 3000) }, (err) => { /* clipboard write failed */ copyTxt.value = err })}</script><template> <div class="m-collapse"> <div class="m-collapse-item" :class="{'u-collapse-item-active': activeJudge(data.key || index)}" v-for="(data, index) in collapseData" :key="index"> <div class="u-collapse-header" @click="onClick(data.key || index)"> <svg focusable="false" v-if="showArrow" class="u-arrow" data-icon="right" aria-hidden="true" viewBox="64 64 896 896"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg> <div class="u-header" :class="{ml24: showArrow}" :style="`font-size: ${fontSize || headerFontSize}px;`"> <slot name="header" :header="data.header" :index="index">{{ data.header || '--' }}</slot> </div> </div> <div class="u-collapse-content" :class="{'u-collapse-copyable': copyable}" :style="`height: ${activeJudge(data.key || index) ? collapseHeight[index]:0}px;`"> <div class="u-lang"> <slot name="lang" :lang="lang" :key="data.key || index">{{ lang }}</slot> </div> <Button size="small" class="u-copy" type="primary" @click="onCopy(index)">{{ copyTxt }}</Button> <div ref="text" class="u-text" :style="`font-size: ${fontSize || textFontSize}px;`"> <slot name="text" :text="data.text" :key="data.key || index">{{ data.text }}</slot> </div> </div> </div> </div></template><style lang="less" scoped>* { box-sizing: border-box; margin: 0; padding: 0;}.m-collapse { background-color: rgba(0, 0, 0, 0.02); border: 1px solid #d9d9d9; border-bottom: 0; border-radius: 8px; .m-collapse-item { border-bottom: 1px solid #d9d9d9; &:last-child { border-radius: 0 0 8px 8px; .u-collapse-content { border-radius: 0 0 8px 8px; } } .u-collapse-header { position: relative; padding: 12px 16px; cursor: pointer; transition: all 0.3s; .u-arrow { position: absolute; width: 12px; height: 12px; top: 0; bottom: 0; margin: auto 0; fill: rgba(0,0,0,.88); transition: transform 0.3s; } .u-header { display: inline-block; color: rgba(0, 0, 0, 0.88); line-height: 1.5714285714285714; } .ml24 { margin-left: 24px; } } .u-collapse-content { position: relative; height: 0; overflow: hidden; background-color: #fff; transition: height .3s; .u-lang { position: absolute; right: 10px; top: 6px; font-size: 14px; color: rgba(0, 0, 0, .38); opacity: 1; transition: opacity .3s; } .u-copy { position: absolute; right: 8px; top: 8px; opacity: 0; pointer-events: none; transition: opacity .3s; } .u-text { padding: 16px; color: rgba(0, 0, 0, 0.88); white-space: pre-wrap; } } .u-collapse-copyable { &:hover { .u-lang { opacity: 0; pointer-events: none; } .u-copy { opacity: 1; pointer-events: auto; } } } } .u-collapse-item-active { .u-arrow { transform: rotate(90deg); } .u-collapse-content { border-top: 1px solid #d9d9d9; } }}</style>

②在要使用的页面引入:

<script setup lang="ts">import { Collapse } from './Collapse.vue'import { ref, watchEffect } from 'vue'const collapseData = ref([ { key: '1', header: 'This is panel header 1', text: 'A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.' }, { key: '2', header: 'This is panel header 2', text: ` A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world. A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.` }, { key: '3', header: 'This is panel header 3', text: 'A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.' }])const activeKey = ref(['1'])watchEffect(() => { console.log('activeKey:', activeKey.value)})const key = ref('1')watchEffect(() => { console.log('key:', key.value)})function onChange (key: any) { console.log('change:', key)}</script><template> <div> <h2 class="mb10">Collapse 折叠面板基本使用 (activeKey 传入 number[] | string[],所有面板可同时展开)</h2> <Collapse :collapseData="collapseData" v-model:activeKey="activeKey" @change="onChange" /> <h2 class="mt30 mb10">'手风琴' (只允许单个内容区域展开,只需 activeKey 传入 number | string 即可)</h2> <Collapse :collapseData="collapseData" v-model:activeKey="key" @change="onChange" /> <h2 class="mt30 mb10">可复制面板内容 (copyable)</h2> <Collapse lang="template" copyable :collapseData="collapseData" v-model:activeKey="activeKey" @change="onChange" /> <h2 class="mt30 mb10">使用插槽 slot 自定义 header、lang、text 内容</h2> <Collapse copyable :collapseData="collapseData" v-model:activeKey="activeKey" @change="onChange"> <template #header="{ header, key }"> <span v-if="key==='1'" style="color: burlywood;">burlywood color {{ header }} (key = {{ key }})</span> </template> <template #lang>typescript</template> <template #text="{ text, key }"> <span v-if="key==='1'" style="color: burlywood;">burlywood color {{ text }} (key = {{ key }})</span> </template> </Collapse> <h2 class="mt30 mb10">折叠面板,隐藏箭头图标 (showArrow: false)</h2> <Collapse :show-arrow="false" :collapseData="collapseData" v-model:activeKey="activeKey" @change="onChange"/> </div></template><style lang="less" scoped></style>
本文链接地址:https://www.jiuchutong.com/zhishi/295167.html 转载请保留说明!

上一篇:基于骨骼关键点的动作识别(OpenMMlab学习笔记,附PYSKL相关代码演示)(基于骨骼关键点的动作识别)

下一篇:布罗兹湿地国家公园,英国诺福克郡 (© Steven Docwra/Getty Images)(布兹洛夫)

  • 税务局纳税申报网站官网
  • 小规模纳税人免税会计分录
  • 疫情期间发生的费用进什么科目
  • 融资租出的设备计入什么科目
  • 旅游费用可以报销吗
  • 收到货款做账
  • 堤围防护费什么时候停止征收
  • 跨年暂估收入如何调整
  • 年终销售返利怎么算
  • 施工企业项目管理办法
  • 发票勾选没有确认怎么办
  • 企业利用个体户走账
  • 工资低于3000要申报吗
  • 小规模纳税人累计开票多少转一般纳税人
  • 100万的收入要交多少
  • 季报表利润表本月怎么算
  • 无车承运业务如何开展
  • 加盟商的作用
  • 公司收到银行存款利息收入会计分录
  • 企业所得税汇算清缴表
  • win10专业版版本号2009
  • 预缴税款忘记抵扣
  • key是什么文档
  • 出租车车票可以出卖吗
  • mac如何打开蓝牙
  • js日期字符串转换为指定格式的日期
  • 建筑装饰工程公司取名
  • form表单提交数组
  • php如何实现登录和注册
  • 支付宝小程序跳回app
  • php网页编程
  • 企业所得税核定征收方法有哪两种
  • 小微企业人员认定标准
  • 什么情况借钱
  • php二维数组按某个键值排序
  • php23种设计模式
  • 预收账款的账务处理摘要
  • 企业的应交税金一般通过什么科目核算
  • 上传附件不成功怎么回事
  • if三元表达式
  • sqlserver2008sa用户不能登录
  • db2运维命令
  • 未交增值税如何计提
  • 回扣发票能查出来么
  • 政府补助的分类包括
  • 营改增后劳务派遣公司账务处理
  • 公司临时工的车可以买吗
  • 进项税抵扣销项税算法
  • 公司投资者如何避免风险
  • 以前年度费用怎么记账
  • 社平工资调整补差能补多少
  • 有什么科目需要考试吗英语翻译
  • 自产自销的免税票
  • 发票的几种分类
  • 资产减值准则规范
  • 员工预支工资现金流量
  • sqlserver 索引
  • execve函数
  • 完美解决索尼电视arc无声音
  • sql数据库检查命令
  • solaris 磁盘管理
  • 资源管理器 windows
  • centos查看文件目录
  • win7系统怎么给软件打开摄像头权限
  • ini文件是什么意思
  • centos部署django项目
  • win8无法识别存档文件
  • Windows设置在哪里
  • jquery()
  • php九宫格
  • 问题少年特训学校
  • 黑客dos攻击是什么攻击
  • perl常用函数
  • Node.js中的事件循环是什么
  • linux shell脚本攻略(第3版)
  • python标准库参考手册
  • js数组添加元素的方法
  • dom基础知识
  • python os.walk遍历目录
  • 最近领导故意挑事
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设