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

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

相关文章

jquery 动画

jquery 动画

jQuery 动画基础 jQuery 提供了一系列动画方法,用于实现元素的动态效果,如淡入淡出、滑动、自定义动画等。 常用动画方法 fadeIn() / fadeOut(): 元素的淡入和…

vue动画实现原理

vue动画实现原理

Vue动画实现原理 Vue的动画系统基于CSS过渡和动画,结合Vue的响应式系统,提供声明式的方式管理元素进入/离开的动画效果。核心原理分为以下几个部分: 过渡类名自动应用 Vue在元素插入/删除时…

vue中实现动画

vue中实现动画

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

vue动画实现跑动

vue动画实现跑动

Vue动画实现跑动效果 在Vue中实现跑动动画可以通过多种方式完成,包括使用Vue的过渡系统、CSS动画或第三方动画库。以下是几种常见的实现方法: 使用Vue过渡和CSS动画 定义CSS关键帧动画来…

vue实现弹出侧边弹窗

vue实现弹出侧边弹窗

使用 Vue 实现侧边弹窗 基础组件结构 创建一个名为 SideDrawer.vue 的组件,包含模板、样式和逻辑部分。弹窗通过 v-model 控制显隐,支持动画过渡。 <template&…

vue实现切换路由动画

vue实现切换路由动画

路由动画的基本实现 在Vue中实现路由切换动画,通常结合<transition>或<transition-group>组件与Vue Router完成。以下是基础实现方式: &…