当前位置:首页 > VUE

vue列表收缩展开实现

2026-02-25 08:33:49VUE

使用 v-show 指令实现展开/收缩

通过 v-show 控制元素的显示与隐藏,适合频繁切换的场景。
在 Vue 模板中绑定布尔值,点击按钮切换状态:

<template>
  <div>
    <button @click="isExpanded = !isExpanded">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
    <ul v-show="isExpanded">
      <li v-for="item in list" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      list: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' }
      ]
    }
  }
}
</script>

使用 v-if 指令实现条件渲染

v-if 会完全销毁或重建 DOM 元素,适合不频繁切换的场景:

<template>
  <div>
    <button @click="toggleList">
      {{ showList ? '收起' : '展开' }}
    </button>
    <ul v-if="showList">
      <li v-for="item in items" :key="item">{{ item }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showList: false,
      items: ['苹果', '香蕉', '橙子']
    }
  },
  methods: {
    toggleList() {
      this.showList = !this.showList
    }
  }
}
</script>

动态高度过渡动画

通过 CSS 过渡实现平滑的展开/收缩效果:

<template>
  <div>
    <button @click="toggle">切换</button>
    <div class="expandable" :style="{ height: expanded ? 'auto' : '0' }">
      <div class="content">
        <p>这里是可展开的内容</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      expanded: false
    }
  },
  methods: {
    toggle() {
      this.expanded = !this.expanded
    }
  }
}
</script>

<style>
.expandable {
  overflow: hidden;
  transition: height 0.3s ease;
}
.content {
  padding: 10px;
}
</style>

使用 Vue Transition 组件

利用 Vue 内置的 <transition> 实现更复杂的动画效果:

<template>
  <div>
    <button @click="show = !show">切换</button>
    <transition name="slide">
      <ul v-if="show" class="list">
        <li v-for="n in 5" :key="n">项目 {{ n }}</li>
      </ul>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false
    }
  }
}
</script>

<style>
.slide-enter-active, .slide-leave-active {
  transition: all 0.5s ease;
}
.slide-enter, .slide-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}
.list {
  margin-top: 10px;
}
</style>

多级嵌套列表展开

处理嵌套列表的展开/收缩功能:

<template>
  <div>
    <ul>
      <li v-for="category in categories" :key="category.id">
        <div @click="toggleCategory(category)">
          {{ category.name }}
          <span>[{{ category.expanded ? '-' : '+' }}]</span>
        </div>
        <ul v-show="category.expanded">
          <li v-for="item in category.items" :key="item">
            {{ item }}
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categories: [
        {
          id: 1,
          name: '水果',
          expanded: false,
          items: ['苹果', '香蕉']
        },
        {
          id: 2,
          name: '蔬菜',
          expanded: false,
          items: ['胡萝卜', '西红柿']
        }
      ]
    }
  },
  methods: {
    toggleCategory(category) {
      category.expanded = !category.expanded
    }
  }
}
</script>

以上方法可根据实际需求选择或组合使用,v-show 适合频繁切换,v-if 适合条件渲染,过渡动画可提升用户体验。

vue列表收缩展开实现

标签: 列表vue
分享给朋友:

相关文章

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Des…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会…

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双向…