当前位置:首页 > 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实现菜单

vue实现菜单

Vue 实现菜单的方法 在 Vue 中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态生成菜单项 通过数据驱动的方式,利用 v-for 指令动态渲染菜单项。定义一个数…

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressor…

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:用…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…