当前位置:首页 > VUE

vue实现展开动画

2026-01-21 19:25:39VUE

使用 Vue Transition 组件实现展开动画

Vue 内置的 Transition 组件可以方便地实现元素的展开/收起动画效果。通过添加 CSS 过渡类名,定义进入和离开的动画过程。

vue实现展开动画

<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <Transition name="expand">
      <div v-show="isExpanded" class="content">
        Content to be expanded
      </div>
    </Transition>
  </div>
</template>

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

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

.expand-enter-from, .expand-leave-to {
  height: 0;
}

.expand-enter-to, .expand-leave-from {
  height: 100px; /* 设置内容实际高度 */
}
</style>

使用动态高度实现自动计算

当内容高度不确定时,可以使用 JavaScript 动态计算高度,实现更灵活的展开动画。

vue实现展开动画

<template>
  <div>
    <button @click="toggleExpand">Toggle</button>
    <div class="container">
      <div ref="content" class="content" :style="{ height: currentHeight }">
        <p>Dynamic content here...</p>
        <p>Line 2</p>
        <p>Line 3</p>
      </div>
    </div>
  </div>
</template>

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

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

.content {
  padding: 10px;
  background: #f5f5f5;
}
</style>

使用第三方动画库(如 GSAP)

对于更复杂的动画效果,可以集成 GSAP 等专业动画库。

<template>
  <div>
    <button @click="toggleExpand">Toggle</button>
    <div ref="content" class="content">
      <p>Content with GSAP animation</p>
    </div>
  </div>
</template>

<script>
import gsap from 'gsap'

export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggleExpand() {
      this.isExpanded = !this.isExpanded
      if (this.isExpanded) {
        gsap.to(this.$refs.content, {
          height: 'auto',
          duration: 0.3,
          ease: 'power2.out'
        })
      } else {
        gsap.to(this.$refs.content, {
          height: 0,
          duration: 0.3,
          ease: 'power2.in'
        })
      }
    }
  },
  mounted() {
    gsap.set(this.$refs.content, { height: 0 })
  }
}
</script>

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

使用 VueUse 的 useTransition 组合式 API

VueUse 库提供了更便捷的动画解决方案。

<template>
  <div>
    <button @click="isExpanded = !isExpanded">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 isExpanded = ref(false)
    const source = ref(0)
    const target = ref(100) // 目标高度

    const height = useTransition(source, {
      duration: 300,
      transition: [0.25, 0.1, 0.25, 1] // CSS easing function
    })

    watch(isExpanded, (val) => {
      source.value = val ? target.value : 0
    })

    return { isExpanded, height }
  }
}
</script>

<style>
.content {
  overflow: hidden;
  background: #f5f5f5;
  padding: 10px;
}
</style>

注意事项

  • 确保动画元素设置了 overflow: hidden 属性,防止内容溢出
  • 对于性能优化,考虑使用 will-change: height 属性
  • 在移动设备上,添加 -webkit-overflow-scrolling: touch 改善滚动体验
  • 动画持续时间建议在 200-300ms 之间,以获得最佳用户体验

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

相关文章

vue实现多选联动

vue实现多选联动

vue实现多选联动的方法 使用v-model绑定数组 在Vue中,可以通过v-model绑定一个数组来实现多选联动。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template&…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templ…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Router 实…