当前位置:首页 > VUE

vue实现slidetoggle

2026-03-27 18:05:05VUE

实现 SlideToggle 效果

在 Vue 中实现 SlideToggle 效果可以通过 CSS 过渡或 JavaScript 动画库完成。以下是几种常见方法:

使用 CSS Transition 和 v-show

通过 Vue 的 v-show 指令结合 CSS 过渡实现滑动效果:

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div class="slide-container" ref="slideContainer">
      <div class="slide-content" v-show="isVisible">
        Content to slide toggle
      </div>
    </div>
  </div>
</template>

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

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

.slide-content[v-show="true"] {
  max-height: 500px; /* 设置为足够大的值 */
}
</style>

使用 Vue Transition 组件

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

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <transition name="slide">
      <div class="content" v-show="isVisible">
        Content to slide toggle
      </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>

使用第三方库(如 Velocity.js)

对于更复杂的动画效果,可以集成动画库:

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div ref="slideContent" style="overflow: hidden">
      Content to slide toggle
    </div>
  </div>
</template>

<script>
import Velocity from 'velocity-animate'

export default {
  data() {
    return {
      isVisible: false
    }
  },
  methods: {
    toggle() {
      this.isVisible = !this.isVisible
      Velocity(
        this.$refs.slideContent,
        this.isVisible ? 'slideDown' : 'slideUp',
        { duration: 300 }
      )
    }
  }
}
</script>

动态计算高度实现精确动画

当需要精确控制元素的实际高度时:

vue实现slidetoggle

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div class="slide-container" :style="{ height: currentHeight + 'px' }">
      <div ref="content">
        Content to slide toggle
      </div>
    </div>
  </div>
</template>

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

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

以上方法可以根据具体需求选择使用,CSS 过渡方案适合简单场景,而 JavaScript 动画库则能提供更丰富的效果和控制能力。

标签: vueslidetoggle
分享给朋友:

相关文章

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件:…

react实现vue

react实现vue

React 实现 Vue 功能 React 和 Vue 是两种不同的前端框架,但可以通过一些方法在 React 中实现 Vue 的特性。以下是几种常见 Vue 功能在 React 中的实现方式: 双…

vue弹幕实现

vue弹幕实现

Vue弹幕实现方法 弹幕功能常见于视频直播或评论区,以下是基于Vue的实现方案,涵盖基础弹幕、动画控制及性能优化。 基础弹幕渲染 通过v-for动态渲染弹幕列表,结合CSS实现横向移动效果。…