当前位置:首页 > VUE

vue实现slideup效果例子

2026-02-23 17:18:15VUE

Vue 实现 SlideUp 效果示例

在 Vue 中实现 SlideUp 效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法:

使用 CSS Transition 和 Vue 的 v-if/v-show

通过 Vue 的 v-ifv-show 控制元素显示隐藏,结合 CSS 过渡实现滑动效果。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition name="slide">
      <div v-if="isVisible" class="content">
        This content will slide up and down
      </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: all 0.5s ease;
}
.slide-enter, .slide-leave-to {
  transform: translateY(100%);
  opacity: 0;
}
.content {
  height: 100px;
  background: lightblue;
  margin-top: 10px;
}
</style>

使用 Vue Transition 的 JavaScript 钩子

通过 JavaScript 钩子实现更复杂的动画控制。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition
      @before-enter="beforeEnter"
      @enter="enter"
      @leave="leave"
      :css="false"
    >
      <div v-if="isVisible" class="content">
        JavaScript hook controlled animation
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isVisible: false
    }
  },
  methods: {
    toggle() {
      this.isVisible = !this.isVisible
    },
    beforeEnter(el) {
      el.style.transform = 'translateY(100%)'
      el.style.opacity = '0'
    },
    enter(el, done) {
      const animation = el.animate([
        { transform: 'translateY(100%)', opacity: 0 },
        { transform: 'translateY(0)', opacity: 1 }
      ], {
        duration: 500
      })
      animation.onfinish = done
    },
    leave(el, done) {
      const animation = el.animate([
        { transform: 'translateY(0)', opacity: 1 },
        { transform: 'translateY(100%)', opacity: 0 }
      ], {
        duration: 500
      })
      animation.onfinish = done
    }
  }
}
</script>

使用第三方动画库(如 Animate.css)

结合 Animate.css 可以快速实现各种预定义动画效果。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition
      enter-active-class="animate__animated animate__slideInUp"
      leave-active-class="animate__animated animate__slideOutDown"
    >
      <div v-if="isVisible" class="content">
        Content with Animate.css
      </div>
    </transition>
  </div>
</template>

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

使用 GSAP 实现高级动画

GSAP 提供了更强大的动画控制能力。

<template>
  <div>
    <button @click="toggle">Toggle Slide</button>
    <transition @enter="enter" @leave="leave" :css="false">
      <div v-if="isVisible" class="content">
        GSAP powered animation
      </div>
    </transition>
  </div>
</template>

<script>
import gsap from 'gsap'
export default {
  data() {
    return {
      isVisible: false
    }
  },
  methods: {
    toggle() {
      this.isVisible = !this.isVisible
    },
    enter(el, done) {
      gsap.from(el, {
        y: '100%',
        opacity: 0,
        duration: 0.5,
        onComplete: done
      })
    },
    leave(el, done) {
      gsap.to(el, {
        y: '100%',
        opacity: 0,
        duration: 0.5,
        onComplete: done
      })
    }
  }
}
</script>

关键点说明

  • CSS Transition 方法最简单,适合基础需求
  • JavaScript 钩子提供更精细的控制
  • Animate.css 适合快速实现预定义动画
  • GSAP 适合复杂动画场景

每种方法都可以根据项目需求选择,简单的 UI 交互推荐使用 CSS Transition,复杂动画场景考虑 GSAP 等专业动画库。

vue实现slideup效果例子

标签: 例子效果
分享给朋友:

相关文章

vue实现吸附效果

vue实现吸附效果

Vue实现吸附效果的方法 吸附效果通常指页面滚动时,某个元素固定在特定位置(如顶部或底部)。以下是几种实现方式: 使用CSS的position: sticky 通过CSS的sticky定位实现吸附效…

vue实现框架效果

vue实现框架效果

Vue实现框架效果的方法 使用Vue实现框架效果可以通过多种方式完成,包括使用Vue Router、动态组件或第三方UI库。以下是几种常见的方法: 使用Vue Router实现布局框架 Vue R…

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 Vue.js 提供了多种方式来实现动态效果展示,以下是一些常见的方法: 使用 Vue 的过渡和动画系统 Vue 内置了 <transition> 和 <…

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果通常涉及动态样式绑定、动画过渡或第三方库的使用。以下是几种常见实现方式: 动态类名绑定 通过 v-bind:class 或简写 :class 实…

vue实现凤凰效果

vue实现凤凰效果

Vue 实现凤凰效果 凤凰效果通常指一种视觉动画,模拟凤凰展翅、火焰燃烧等动态效果。在 Vue 中实现这种效果,可以结合 CSS 动画、Canvas 或第三方动画库(如 GSAP)。以下是几种实现方式…