vue实现手风琴

原理:为每一个项传入特定序号,实现特定项的展示(手风琴效果)
(使用vue-cli 可直接复制查看效果)
<template>
  <!--手风琴组件-->
  <div class="hello">
    <ul>
      <li>
        <span @click="toggle(1)">一条</span>
        <div v-show="show===1">
          一万
        </div>
      </li>
      <li>
        <span @click="toggle(2)">二条</span>
        <div v-show="show===2">
          贰万
        </div>
      </li>
      <li>
        <span @click="toggle(3)">三条</span>
        <div v-show="show===3">
          叁万
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      show:''
    }
  },
  methods:{
    toggle:function(index){
      if(this.show===index){
        this.show=0;
      }
      else{
        this.show=index;
      }
    }
  }
}
</script>
### Vue3 实现手风琴式弹窗组件 在构建基于 Vue3 的手风琴式弹窗组件时,可以利用 Vue3 提供的组合 API 和响应式特性来简化开发过程。下面是一个简单的实现方案。 #### 创建基础结构 首先定义一个名为 `Accordion` 的组件: ```html <template> <div class="accordion"> <div v-for="(item, index) in items" :key="index" @click="toggle(index)"> <header>{{ item.title }}</header> <transition name="fade"> <section v-if="activeIndex === index">{{ item.content }}</section> </transition> </div> </div> </template> <script setup> import { ref } from 'vue'; const props = defineProps({ items: { type: Array, required: true } }); let activeIndex = ref(null); function toggle(index) { if (activeIndex.value === index) { activeIndex.value = null; } else { activeIndex.value = index; } } </script> <style scoped> .fade-enter-active, .fade-leave-active { transition: max-height 0.3s ease-out; } .fade-enter-from, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ { max-height: 0; } .fade-enter-to, .fade-leave-from { max-height: 50px; /* Adjust based on content */ } </style> ``` 此代码片段展示了如何创建一个基本的手风琴组件[^1]。通过点击标题部分展开或折叠对应的面板内容,并应用了一个简单的 CSS 过渡动画以增强用户体验。 为了使该组件更加实用,在实际项目中可能还需要考虑更多细节,比如支持多级嵌套、自定义样式以及与其他 UI 库集成等功能扩展。 #### 使用示例 要在页面上使用这个手风琴组件,只需将其导入并传递适当的数据即可: ```html <template> <div id="app"> <Accordion :items="data"/> </div> </template> <script setup> import Accordion from './components/Accordion.vue'; import { reactive } from 'vue'; // 示例数据 const data = reactive([ { title: 'Section One', content: 'Content of section one.' }, { title: 'Section Two', content: 'Content of section two.' }, { title: 'Section Three', content: 'Content of section three.' } ]); </script> ``` 上述例子说明了如何配置和初始化手风琴组件及其所需的内容项数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值