vue3实现折叠面板

在这里插入图片描述
总是忘记vue怎么实现折叠面板,记录一下,没有动画样式。

 <div class="item-content padding15" style="font-size: 0.875rem;" @click="changeCollapse(index)" v-for="(item, index) in collData.dataList" :key="index" >
	<div class="flex justify-between items-center">
	  <div>
	    <span style="color: var(--fontColor);">{{ item.first }}</span>至
	    <span style="color: var(--fontColor);">{{ item.last }}</span>(8)
	  </div>
	  <el-icon v-if="collData.activeIndex.indexOf(index) != -1"><ArrowDown color="#262626" /></el-icon>
	  <el-icon v-else><ArrowRight color="#262626" /></el-icon>
	</div>
	<div class="gradient-wrapper" :class="collData.activeIndex.indexOf(index) != -1?'active':''">
	  <div class="marginT20" v-for="(org, ind) in item.orgList" :key="ind">
	    <div class="flex justify-between items-center padding10 marginB15 item-get-org">
	      <div class="item-get-name ellipsis">{{ org.name }}({{ org.unitName }})</div>
	      <el-icon :size="18" class="pointer"><CircleClose color="#AAAAAA" /></el-icon>
	    </div>
	  </div>
	</div>
	</div>

<script setup>
import {ref, reactive} from "vue";
const collData = reactive({
  dataList: [{
    first: 'k2',
    last: 'k12',
    orgList: [{
      name: '张三',
      unitName: '某某单位'
    }]
  },
  {
    first: 'k2',
    last: 'k12',
    orgList: [{
      name: '张三2',
      unitName: '某某单位'
    }]
  }],
  activeIndex: []
})
const changeCollapse = (index) => {
  if (collData.activeIndex.indexOf(index) == -1) {
    collData.activeIndex.push(index)
  } else {
    collData.activeIndex = collData.activeIndex.filter(item => item != index)
  }
}
</script>

<style scoped>
.item-content {
  margin: 15px 0;
  background: #f8f8f8;
  border-radius: 4px;
}
.item-unit-add {
  display: flex;
  flex-wrap: wrap;    
  padding: 10px 0;
}
.item-unit {
  padding-left: 15px;
  padding-right: 10px;
  margin: 5px;
  width: 96px;
  height: 40px;
  line-height: 40px;
  background: #F4F4F4;
  border: 1px solid #AAAAAA;
  border-radius: 4px;
  font-size: 0.875rem;
}
.item-get-org {
  background: #F4F4F4;
  border: 1px solid #AAAAAA;
  border-radius: 4px;
  font-size: 0.875rem;
}
.item-get-org:last-child {
  margin-bottom: 0;
}
.item-get-name {
  width: 80%;
}

.gradient-wrapper {
  height: 0;
  overflow: hidden;
}
.gradient-wrapper.active {
  height: 100%;
  transition: height .3s ease-in;
  transform-origin: 50% 0;
  animation: slide-down 0.3s ease-in;
  -webkit-animation: slide-down 0.3s ease-in;
}

@keyframes slide-down {
  0% {
    opacity: 0;
  }
  30% {
    opacity: .3;
  }
  60% {
    opacity: .6;
  }
  100% {
    opacity: .6;
  }
}

@-webkit-keyframes slide-down {
  0% {
    opacity: 0;
  }
  30% {
    topacity: .3;
  }
  60% {
    opacity: .6;
  }
  100% {
    opacity: .6;
  }
}
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Vue3实现折叠面板动画的示例代码: ```html <template> <div class="accordion"> <div class="accordion-item" v-for="(item, index) in items" :key="index"> <div class="accordion-header" @click="toggle(index)"> {{ item.title }} <span class="icon" :class="{ 'is-active': item.open }"></span> </div> <div class="accordion-content" :style="{ height: item.open ? item.height + 'px' : 0 }" @transitionend="transitionEnd(index)"> <div class="inner"> {{ item.content }} </div> </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const items = ref([ { title: '折叠面板1', content: '这是折叠面板1的内容', open: false, height: 0, }, { title: '折叠面板2', content: '这是折叠面板2的内容', open: false, height: 0, }, { title: '折叠面板3', content: '这是折叠面板3的内容', open: false, height: 0, }, ]); const toggle = (index) => { items.value[index].open = !items.value[index].open; if (items.value[index].open) { items.value[index].height = document.querySelector(`.accordion-item:nth-child(${index + 1}) .accordion-content .inner`).offsetHeight; } else { items.value[index].height = 0; } }; const transitionEnd = (index) => { if (!items.value[index].open) { items.value[index].height = 0; } }; return { items, toggle, transitionEnd, }; }, }; </script> <style> .accordion { .accordion-item { .accordion-header { cursor: pointer; .icon { display: inline-block; width: 10px; height: 10px; margin-left: 10px; border-top: 2px solid #333; border-right: 2px solid #333; transform: rotate(45deg); transition: transform 0.3s ease; &.is-active { transform: rotate(-135deg); } } } .accordion-content { overflow: hidden; transition: height 0.3s ease; .inner { padding: 10px; } } } } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值