当前位置:首页 > 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 工具函数,可以更精细地控制过渡效果。

<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

vue实现slidetoggle

标签: vueslidetoggle
分享给朋友:

相关文章

vue实现a

vue实现a

Vue 实现 A 功能的方法 在 Vue 中实现 A 功能可以通过多种方式,具体取决于 A 功能的具体需求。以下是几种常见的实现方法: 使用 Vue 组件 通过创建自定义组件来实现 A 功能。组件…

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue实现引导

vue实现引导

Vue 实现引导功能的方法 使用第三方库 推荐使用 driver.js 或 intro.js 这类专门为 Vue 设计的引导库。这些库提供了完整的 API 和样式,能够快速实现引导功能。 安装 dr…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…