位置: 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)(布兹洛夫)

  • 小规模企业需要交哪些税
  • 销项税转出是啥意思
  • 季度所得税计提
  • 企业税收主要分为哪几类
  • 一般纳税人结转税额怎么做会计分录
  • 应纳税所得额可以扣除哪些
  • 财政部监制章可以报销吗
  • 可辨认净资产和所有者权益的区别
  • 预授权撤销怎么撤销
  • 辞退员工补偿金账务处理
  • 当期免抵税额有什么用
  • 企业基本保险
  • 商户办会员卡 商场承担责任
  • 关于税控清卡清算的报告
  • 金税四期再出新公告
  • 其他综合收益可以转损益的情况
  • 结息的会计分录怎么写
  • 销售单怎么设置打几行
  • 如何解决无线网络连接问题
  • 技嘉主板bios更新后无法开机
  • 弥补以前年度亏损是什么意思
  • 小规模企业年末怎么结转
  • bios怎么恢复出厂设置dell
  • win11透明任务栏怎么设置
  • 个体户经营所得核定税率
  • jQuery+Ajax+PHP“喜欢”评级功能实现代码
  • 教学用品列入什么费用
  • 记账凭证技巧
  • 房产自用或出租什么意思
  • 目前超频最高几ghz
  • php实现图片上传的方法
  • 成本费用科目包括
  • css中文字垂直排列
  • php7 openssl
  • thinkphp百万级数据查询
  • php源码抓取工具
  • 会计调整以前年度遗留问题查不出来说明怎么写
  • 加工行业增值税负率一般控制在多少?
  • 购进小汽车自用为什么可以抵扣进项税
  • 未达到起征点的增值税怎么填写
  • 先付款后开票怎么做账务处理
  • html前端开发教程
  • 研发费用凭证是什么样
  • 向个体工商户付款可以转给个体法人吗
  • 税控系统如何清卡
  • 房屋租赁的发票能抵扣吗
  • 建筑工程公司需要哪些资质证书
  • 从公司账户转给出纳备注
  • 公司租用房屋用交房产税吗
  • 收不回来的应收账款会计分录
  • 出纳提取现金有金额限制吗
  • 3%税率专票为什么不能抵扣
  • 专利申请费用能走工会会费吗
  • 预收账款占销售收入比例预警值为比例大于
  • 货款尚未收到属于什么会计科目
  • 税收会计采用什么记账法
  • mysql分区分表原理
  • sql面试题50题
  • macappstore下载不了软件
  • centos 7 远程桌面
  • 在Linux系统中如何打开R语言控制台
  • linux的文件结构
  • Win7开机就蓝屏
  • linux系统怎样
  • ExtJS4 动态生成的grid导出为excel示例
  • cocos2dx 4.0
  • AngularJS + Node.js + MongoDB开发的基于高德地图位置的通讯录
  • c# unity 教程
  • three.js入门教程(合集)
  • 如何做好设计师
  • unity获取鼠标
  • mongodb python
  • js全局变量怎么定义
  • 全国税务查询
  • 财政票据和税务票据的法律效力一样吗
  • 成都税务局发票查询
  • 江西省国家税务局
  • 新车缴纳车船税减免性质是什么
  • 增值税普票十万怎么开
  • 出口退税函调不予退税的后果
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设