当前位置:首页 > VUE

vue动画实现折叠

2026-03-29 00:13:03VUE

vue动画实现折叠

在Vue中实现折叠动画,可以使用内置的<transition>组件结合CSS过渡或JavaScript钩子。以下是两种常见方法:

vue动画实现折叠

使用CSS过渡

通过Vue的<transition>组件和CSS的max-height属性实现平滑的折叠效果。

vue动画实现折叠

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <transition name="slide">
      <div v-show="isOpen" class="content">
        Content to be folded
      </div>
    </transition>
  </div>
</template>

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

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.3s ease;
  overflow: hidden;
}

.slide-enter, .slide-leave-to {
  max-height: 0;
}

.slide-enter-to, .slide-leave {
  max-height: 1000px; /* Adjust based on content height */
}

.content {
  background: #f0f0f0;
}
</style>

使用JavaScript钩子

通过<transition>的JavaScript钩子动态计算高度,适合内容高度不确定的情况。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <transition
      @enter="enter"
      @leave="leave"
      :css="false"
    >
      <div v-show="isOpen" class="content" ref="content">
        Content with dynamic height
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isOpen: false
    }
  },
  methods: {
    toggle() {
      this.isOpen = !this.isOpen
    },
    enter(el, done) {
      el.style.height = 'auto'
      const height = el.scrollHeight
      el.style.height = '0'
      setTimeout(() => {
        el.style.height = `${height}px`
        el.addEventListener('transitionend', done)
      }, 0)
    },
    leave(el, done) {
      el.style.height = `${el.scrollHeight}px`
      setTimeout(() => {
        el.style.height = '0'
        el.addEventListener('transitionend', done)
      }, 0)
    }
  }
}
</script>

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

使用第三方库(如VueUse)

借助@vueuse/coreuseTransition实现更复杂的动画效果。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div class="content" :style="{ height: `${height}px` }">
      Content with VueUse transition
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useTransition } from '@vueuse/core'

export default {
  setup() {
    const isOpen = ref(false)
    const contentHeight = ref(0)
    const height = useTransition(contentHeight, {
      duration: 300,
      transition: [0.75, 0, 0.25, 1]
    })

    const toggle = () => {
      isOpen.value = !isOpen.value
      contentHeight.value = isOpen.value ? 200 : 0 // Adjust height as needed
    }

    return { isOpen, toggle, height }
  }
}
</script>

<style>
.content {
  overflow: hidden;
  background: #f0f0f0;
}
</style>

以上方法均可实现折叠动画,选择取决于项目需求。CSS过渡适合简单场景,JavaScript钩子提供更精细控制,而第三方库能简化复杂动画的实现。

标签: 动画vue
分享给朋友:

相关文章

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…

vue实现必填

vue实现必填

Vue 实现表单必填验证的方法 在Vue中实现表单必填验证可以通过多种方式完成,以下是几种常见的方法: 使用HTML5原生属性 最简单的必填验证可以通过HTML5的required属性实现: &l…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…