当前位置:首页 > VUE

vue实现弹出动画

2026-02-24 05:08:56VUE

使用 Vue Transition 组件实现弹出动画

Vue 提供了内置的 Transition 组件,可以方便地为元素添加进入/离开的过渡效果。以下是一个基本的弹出动画实现:

<template>
  <button @click="show = !show">Toggle</button>
  <Transition name="pop">
    <div v-if="show" class="popup">Popup Content</div>
  </Transition>
</template>

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

<style>
.popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
  background: white;
  box-shadow: 0 0 10px rgba(0,0,0,0.2);
}

.pop-enter-active,
.pop-leave-active {
  transition: all 0.3s ease;
}

.pop-enter-from,
.pop-leave-to {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.5);
}
</style>

使用 CSS 动画实现弹跳效果

如果需要更有弹性的动画效果,可以使用 CSS keyframes:

vue实现弹出动画

.pop-enter-active {
  animation: bounce-in 0.5s;
}
.pop-leave-active {
  animation: bounce-in 0.5s reverse;
}

@keyframes bounce-in {
  0% {
    transform: translate(-50%, -50%) scale(0);
    opacity: 0;
  }
  50% {
    transform: translate(-50%, -50%) scale(1.1);
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
}

使用第三方动画库

对于更复杂的动画效果,可以集成第三方动画库如 Animate.css:

vue实现弹出动画

<template>
  <Transition
    enter-active-class="animate__animated animate__bounceIn"
    leave-active-class="animate__animated animate__bounceOut"
  >
    <div v-if="show" class="popup">Popup Content</div>
  </Transition>
</template>

<script>
import 'animate.css'
export default {
  data() {
    return {
      show: false
    }
  }
}
</script>

实现模态框弹出动画

结合 Vue 的 Teleport 组件实现模态框弹出效果:

<template>
  <button @click="showModal = true">Open Modal</button>

  <Teleport to="body">
    <Transition name="modal">
      <div v-if="showModal" class="modal-mask">
        <div class="modal-container">
          <h3>Modal Title</h3>
          <p>Modal Content</p>
          <button @click="showModal = false">Close</button>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-container {
  background: white;
  padding: 20px;
  border-radius: 8px;
  max-width: 80%;
}

.modal-enter-from,
.modal-leave-to {
  opacity: 0;
}

.modal-enter-active .modal-container,
.modal-leave-active .modal-container {
  transition: transform 0.3s ease;
}

.modal-enter-from .modal-container,
.modal-leave-to .modal-container {
  transform: scale(0.8);
}
</style>

使用 Vue 3 的 Composition API 实现动画控制

在 Vue 3 中可以使用 Composition API 更精细地控制动画:

<template>
  <button @click="toggle">Toggle</button>
  <Transition @before-enter="beforeEnter" @enter="enter" @leave="leave">
    <div v-if="show" class="box"></div>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'

const show = ref(false)

function toggle() {
  show.value = !show.value
}

function beforeEnter(el) {
  el.style.opacity = 0
  el.style.transform = 'scale(0)'
}

function enter(el, done) {
  const animation = el.animate([
    { opacity: 0, transform: 'scale(0)' },
    { opacity: 1, transform: 'scale(1.1)' },
    { opacity: 1, transform: 'scale(1)' }
  ], {
    duration: 500
  })
  animation.onfinish = done
}

function leave(el, done) {
  const animation = el.animate([
    { opacity: 1, transform: 'scale(1)' },
    { opacity: 0, transform: 'scale(0)' }
  ], {
    duration: 300
  })
  animation.onfinish = done
}
</script>

标签: 弹出动画
分享给朋友:

相关文章

vue实现共享动画

vue实现共享动画

Vue 共享动画实现方法 使用 Vue Transition 组件 Vue 内置的 Transition 组件可以实现元素进入/离开的过渡效果。通过命名过渡和 mode 属性可以控制多个元素的过渡顺序…

jquery动画

jquery动画

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

jquery 弹出

jquery 弹出

jQuery 弹出窗口实现方法 jQuery 可以通过多种方式实现弹出窗口效果,以下是几种常见的方法: 使用 jQuery UI Dialog jQuery UI 提供了一个 Dialog 组件,可…

vue实现gif动画

vue实现gif动画

在Vue中实现GIF动画 使用<img>标签直接引入GIF文件 在Vue组件中,可以直接通过<img>标签引入GIF文件。确保GIF文件已放置在项目的静态资源目录(如publ…

vue实现路由动画

vue实现路由动画

路由动画的实现方法 在Vue中实现路由动画通常结合<transition>组件和Vue Router的导航守卫。以下是几种常见实现方式: 基础过渡动画 <template>…

vue实现的动画

vue实现的动画

Vue 动画实现方式 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接使用 CSS 动画。以下是几种常见的实现方法: 使用 Vue 过渡系统 Vue 的 <tra…