当前位置:首页 > VUE

vue实现slidetoggle

2026-01-07 22:21:06VUE

Vue 实现 SlideToggle 效果

SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法:

使用 CSS Transition 和 v-show

通过 Vue 的 v-show 指令和 CSS 过渡实现基础滑动效果。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div 
      class="slide-content" 
      v-show="isVisible"
      :style="{ maxHeight: isVisible ? '500px' : '0' }"
    >
      Content to slide toggle
    </div>
  </div>
</template>

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

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

使用 Vue Transition 组件

Vue 内置的 <transition> 组件可以更灵活地控制动画效果。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <transition name="slide">
      <div class="content" v-show="isVisible">
        Slide content here
      </div>
    </transition>
  </div>
</template>

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

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.3s ease;
  overflow: hidden;
}
.slide-enter-from, .slide-leave-to {
  max-height: 0;
}
.slide-enter-to, .slide-leave-from {
  max-height: 500px;
}
</style>

使用第三方库(如 VueUse)

VueUse 提供了 useTransition 工具函数,可以更精细地控制过渡效果。

vue实现slidetoggle

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div 
      class="slide-content" 
      :style="{ height: `${height}px` }"
    >
      Content to slide toggle
    </div>
  </div>
</template>

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

export default {
  setup() {
    const isVisible = ref(false)
    const contentRef = ref(null)

    const height = useTransition(
      isVisible,
      {
        duration: 300,
        transition: [0.25, 0.1, 0.25, 1]
      }
    )

    const toggle = () => {
      isVisible.value = !isVisible.value
    }

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

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

注意事项

  • 确保为滑动元素设置 overflow: hidden 属性
  • 滑动高度应根据实际内容动态计算,避免硬编码
  • 移动端设备可能需要额外考虑性能优化
  • 复杂动画场景建议使用 CSS 硬件加速属性如 transform

标签: vueslidetoggle
分享给朋友:

相关文章

vue实现选择分类

vue实现选择分类

Vue 实现选择分类的方法 使用 v-model 绑定选择值 在 Vue 中可以通过 v-model 指令实现双向数据绑定,适用于下拉选择框(<select>)。以下是一个基本示例: &…

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <i…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…