当前位置:首页 > VUE

vue实现slideUp动画效果

2026-01-23 12:50:31VUE

使用 Vue Transition 实现 SlideUp 动画

Vue 的 <transition> 组件可以结合 CSS 实现 slideUp 效果。通过定义进入和离开的动画类,控制元素的高度和透明度变化。

<template>
  <button @click="show = !show">Toggle Slide</button>
  <transition name="slide-up">
    <div v-if="show" class="box">Content to slide up</div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      show: false
    }
  }
}
</script>

<style>
.slide-up-enter-active,
.slide-up-leave-active {
  transition: all 0.3s ease;
}

.slide-up-enter-from,
.slide-up-leave-to {
  opacity: 0;
  transform: translateY(20px);
}
</style>

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

对于更复杂的动画效果,可以集成 Velocity.js 等动画库。

<template>
  <button @click="show = !show">Toggle Slide</button>
  <transition
    @enter="enter"
    @leave="leave"
    :css="false"
  >
    <div v-if="show" class="box">Content with Velocity.js</div>
  </transition>
</template>

<script>
import Velocity from 'velocity-animate'

export default {
  data() {
    return {
      show: false
    }
  },
  methods: {
    enter(el, done) {
      Velocity(el, { opacity: 1, translateY: 0 }, { duration: 300, complete: done })
    },
    leave(el, done) {
      Velocity(el, { opacity: 0, translateY: '20px' }, { duration: 300, complete: done })
    }
  }
}
</script>

动态高度 SlideUp(适用于不确定高度的元素)

通过 JavaScript 动态计算元素高度,实现更平滑的展开/折叠效果。

<template>
  <button @click="toggle">Toggle Slide</button>
  <div class="slide-container">
    <div ref="content" class="slide-content" :style="{ height }">
      <p>Dynamic content with variable height</p>
    </div>
  </div>
</template>

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

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

使用 CSS Grid 实现 SlideUp

CSS Grid 的 grid-template-rows 属性可以实现平滑的高度过渡效果。

<template>
  <button @click="isOpen = !isOpen">Toggle Grid Slide</button>
  <div class="grid-container" :class="{ 'is-open': isOpen }">
    <div class="grid-content">Content inside CSS Grid</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isOpen: false
    }
  }
}
</script>

<style>
.grid-container {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.3s ease;
}

.grid-container.is-open {
  grid-template-rows: 1fr;
}

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

vue实现slideUp动画效果

标签: 效果动画
分享给朋友:

相关文章

jquery动画

jquery动画

jQuery动画基础 jQuery提供了多种动画方法,用于创建平滑的过渡效果。常用的动画方法包括animate()、fadeIn()、fadeOut()、slideUp()和slideDown()。…

vue动画实现折叠

vue动画实现折叠

使用 Vue Transition 实现折叠动画 在 Vue 中可以通过 <transition> 组件和 CSS 过渡实现折叠效果。核心原理是动态切换元素的 height 属性,配合 o…

vue实现动画特效

vue实现动画特效

Vue 实现动画特效的方法 Vue 提供了多种方式实现动画特效,主要通过内置组件和第三方库结合 CSS 或 JavaScript 完成。以下是常见的实现方法: 使用 Vue 内置过渡组件 Vue…

vue实现轮播效果

vue实现轮播效果

使用Swiper插件实现轮播 安装Swiper及相关依赖: npm install swiper vue-awesome-swiper 组件代码示例: <template> &…

vue实现开关效果

vue实现开关效果

使用Vue实现开关效果 使用v-model绑定数据 通过v-model绑定一个布尔值来控制开关状态。当用户点击开关时,布尔值会自动更新。 <template> <div cl…

vue 实现toggle效果

vue 实现toggle效果

使用 v-show 实现 Toggle 通过 v-show 指令可以控制元素的显示与隐藏,适合频繁切换的场景。v-show 仅切换 CSS 的 display 属性,不会销毁和重建 DOM。 <…