当前位置:首页 > VUE

vue实现展开收缩

2026-03-08 02:43:21VUE

Vue 实现展开收缩功能

使用 v-show 指令

通过 v-show 控制元素的显示与隐藏,适合频繁切换的场景。

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <div v-show="isExpanded">
      This content can be expanded or collapsed.
    </div>
  </div>
</template>

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

使用 v-if 指令

通过 v-if 控制元素的渲染与销毁,适合不频繁切换且需要减少 DOM 节点数的场景。

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <div v-if="isExpanded">
      This content can be expanded or collapsed.
    </div>
  </div>
</template>

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

结合 CSS 过渡动画

通过 Vue 的 transition 组件实现平滑的展开/收缩动画效果。

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <transition name="slide">
      <div v-show="isExpanded" class="content">
        This content has a smooth transition effect.
      </div>
    </transition>
  </div>
</template>

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

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.5s ease;
}
.slide-enter, .slide-leave-to {
  max-height: 0;
  overflow: hidden;
}
.slide-enter-to, .slide-leave {
  max-height: 100px;
}
.content {
  background: #f5f5f5;
  padding: 10px;
}
</style>

动态高度过渡

通过动态计算内容高度实现更自然的展开/收缩效果。

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <div
      class="content"
      :style="{ height: isExpanded ? contentHeight + 'px' : '0' }"
      ref="content"
    >
      <div ref="inner">This content has dynamic height transition.</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      contentHeight: 0
    }
  },
  mounted() {
    this.updateHeight()
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    },
    updateHeight() {
      this.contentHeight = this.$refs.inner.offsetHeight
    }
  }
}
</script>

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

使用第三方组件

对于更复杂的需求,可以考虑使用第三方组件如 vue-collapse 或自定义指令实现。

vue实现展开收缩

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <vue-collapse :show="isExpanded">
      <div class="content">
        This content is managed by a third-party collapse component.
      </div>
    </vue-collapse>
  </div>
</template>

<script>
import { VueCollapse } from 'vue-collapse'

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

以上方法可以根据具体需求选择使用,从简单显示隐藏到复杂动画效果均可实现。

标签: vue
分享给朋友:

相关文章

vue能实现

vue能实现

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

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…

vue实现监控

vue实现监控

Vue 实现监控功能的方法 在 Vue 中实现监控功能通常涉及数据变化监听、事件监听或第三方监控工具集成。以下是几种常见实现方式: 使用 Vue 的 watch 监听数据变化 通过 watch 可以…

vue实现右滑

vue实现右滑

Vue 实现右滑功能 右滑功能通常用于移动端应用,实现手势操作。以下是几种常见的实现方法: 使用 Hammer.js 库 Hammer.js 是一个专门处理手势的库,可以方便地实现右滑功能。 im…

vue界面实现滚动

vue界面实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生CSS、JavaScript或第三方库。以下是一些常见的实现方法: 使用CSS实现滚动 通过CSS的overflow属性可…

vue实现力导向

vue实现力导向

Vue 实现力导向图 在 Vue 中实现力导向图(Force-Directed Graph)通常需要借助第三方库,如 D3.js 或 vis.js。以下是两种常见的实现方式: 使用 D3.js 实现…