当前位置:首页 > VUE

vue实现slidetoggle

2026-01-07 22:21:06VUE

Vue 实现 SlideToggle 效果

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

vue实现slidetoggle

使用 CSS Transition 和 v-show

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

vue实现slidetoggle

<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 工具函数,可以更精细地控制过渡效果。

<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-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue实现肢体识别

vue实现肢体识别

Vue 中实现肢体识别的方法 在 Vue 项目中实现肢体识别通常需要结合第三方库或 API,例如 TensorFlow.js、MediaPipe 或 OpenCV。以下是几种常见的实现方式: 使用…

vue路由实现iframe

vue路由实现iframe

在Vue中实现iframe嵌入可以通过路由配置和组件动态加载来完成。以下是具体实现方法: 路由配置 在Vue Router的路由配置中,通过component属性动态加载iframe组件。需要将目标…