当前位置:首页 > 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:

.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:

<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 更精细地控制动画:

vue实现弹出动画

<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 实现弹出卡片的方法 使用 v-if 或 v-show 控制显示 通过 Vue 的指令 v-if 或 v-show 可以控制弹出卡片的显示和隐藏。v-if 会动态添加或移除 DOM 元素,而 v…

css3制作简单动画

css3制作简单动画

CSS3 制作简单动画的方法 CSS3 提供了多种方式制作动画,主要通过 transition、transform 和 animation 属性实现。以下是几种常见的实现方法。 使用 transit…

vue实现弹出页

vue实现弹出页

Vue 实现弹出页的方法 在Vue中实现弹出页(弹窗/模态框)可以通过多种方式完成,以下是常见的几种方法: 使用组件化方式 创建一个独立的弹窗组件(如Modal.vue),通过v-if或v-show…

vue动画实现方式

vue动画实现方式

Vue 动画实现方式 Vue 提供了多种方式来实现动画效果,主要通过内置的 <transition> 和 <transition-group> 组件以及结合 CSS 或 Jav…

vue中实现动画

vue中实现动画

vue中实现动画的方法 Vue提供了多种实现动画的方式,主要依赖于内置的<transition>和<transition-group>组件,以及结合CSS或JavaScript…

vue实现停止动画

vue实现停止动画

停止动画的方法 在Vue中停止动画可以通过多种方式实现,具体取决于动画的实现方式(CSS动画、JavaScript动画或第三方库如GSAP)。 使用CSS动画 通过动态绑定类名或样式来控制动画的播放…