位置: IT常识 - 正文

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

发布时间:2024-01-14
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)(布兹洛夫)

  • 跨年增票能开红冲吗?
  • 动产租赁适用税率
  • 纳税人识别号是什么哪里可以查到
  • 土地摊销账务处理
  • 公司投资股票亏损,要交企业所得税和个人所得税吗
  • 一季度计提的所得税分录
  • 报废固定资产产生的净损益属于利得吗
  • 银行承兑汇票背书可以拆分
  • 境外付款境内支付什么意思
  • 电子承兑背书一般多久到账
  • 确认主营业务成本的依据是什么单据
  • 企业自建房屋建几层合适
  • 总公司调到子公司
  • 备用金可以银行贷款吗
  • 报表中利润为什么税后比税前大?
  • 未交五险一金的原因
  • 航天发票上传不成功怎么手动上传
  • 按季申报是什么意思
  • 作废发票如何管理
  • 居民企业之间以非货币资产进行投资
  • 企业捐赠现金支出应在哪个项目反应
  • 外企投资应该怎么投资
  • 发票拍照打印出来不清晰
  • 少付的运费如何做分录
  • 企业所得税法的规定可在税前扣除的
  • 建筑总承包企业资质
  • 拿库存商品抵债怎么做账
  • 营改增后定额材料单价的组成包括
  • 戴尔开启vt教程
  • 如何在excel中添加选项内容
  • 演出收入怎样做会计分录
  • 企业退款如何走账
  • iphone6s掉水里怎么办
  • windows11 怎么更新
  • 入库材料未付款属于什么会计科目
  • 一公顷等于多少千米
  • 开发间接费用会计分录
  • 印花税土地使用税和房产税怎么做会计分录
  • 应收账款的主要功能包括哪些
  • 增值税专用发票上注明的价款含税吗
  • 免购车税政策
  • 行政单位基建账如何并入大账
  • ICLR2023《Crossformer: Transformer Utilizing Cross-Dimension Dependency for Multivariate Time Series》
  • ipcrm命令
  • 固定资产的计提折旧在月初还是月末
  • 印花税申报时间填错了怎么处理?
  • 一个小柜的货代费用
  • 新建会计帐套怎么建
  • 织梦如何使用
  • 善意取得增值税专用发票
  • mysql查询日志位置
  • 城建税减半从什么时候开始
  • 公益事业捐赠有哪些税收优惠政策
  • 新收入准则 2021
  • 交易性金融资产的入账价值
  • 对于银行已入账企业未入账的未达账项应该
  • 暂估应付账款的科目编码
  • 采购预支款报销流程
  • 房屋租赁的注意问题
  • 普通发票的金额
  • 申报抵扣的进项税额怎么算
  • 垫付的开办费怎么做账
  • 转出投资款计入现金流量表的哪里
  • 盈余公积是资产类科目吗
  • mysql5.7.29安装
  • ie内存怎么清理
  • Vuforia ImageTarget Native版本编译
  • linux echo echo
  • NGUI学习笔记汇总
  • android 加载更多
  • javascript怎么学
  • python lambda的用法
  • python gensim
  • python中计数函数怎么用
  • javascript面向对象编程指南第三版
  • java图像对比
  • 贵州省 税务局
  • 工会经费按年还是季度申报
  • 自然人扣缴客户端重新安装后信息怎么同步
  • 开通税务在哪里弄
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号