当前位置:首页 > VUE

vue实现收缩展开

2026-01-16 22:33:57VUE

Vue 实现收缩展开功能

在 Vue 中实现收缩展开功能可以通过多种方式实现,常见的有使用 v-showv-if 或结合 CSS 过渡动画。以下是几种实现方法:

使用 v-show 控制显示隐藏

v-show 通过切换 CSS 的 display 属性实现元素的显示和隐藏,适合频繁切换的场景。

<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;
}
</style>

动态高度动画

如果需要更精确的动画效果,可以通过 JavaScript 动态计算内容高度。

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <div ref="content" class="content" :style="{ height: contentHeight }">
      This content has dynamic height animation.
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      contentHeight: '0px'
    };
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded;
      if (this.isExpanded) {
        this.contentHeight = `${this.$refs.content.scrollHeight}px`;
      } else {
        this.contentHeight = '0px';
      }
    }
  }
};
</script>

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

使用第三方库

如果需要更复杂的动画效果,可以结合第三方库如 vue-animateanimate.css

vue实现收缩展开

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <transition
      enter-active-class="animate__animated animate__fadeIn"
      leave-active-class="animate__animated animate__fadeOut"
    >
      <div v-show="isExpanded" class="content">
        This content uses Animate.css for animations.
      </div>
    </transition>
  </div>
</template>

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

<style>
@import 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css';
.content {
  margin-top: 10px;
}
</style>

以上方法可以根据具体需求选择适合的实现方式。

标签: vue
分享给朋友:

相关文章

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

vue实现rgb调色

vue实现rgb调色

Vue实现RGB调色功能 实现思路 通过Vue的数据绑定和事件处理,动态调整RGB三个通道的值,实时更新显示颜色。 核心代码示例 <template> <div…

简单实现vue github

简单实现vue github

实现一个简单的 Vue 项目并上传到 GitHub 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保已安装 Node.js 和 npm/yarn。运行以下命令创建项目:…