当前位置:首页 > VUE

vue3实现动画

2026-02-23 09:04:44VUE

vue3 实现动画的方法

Vue 3 提供了多种实现动画的方式,包括内置的过渡组件、第三方动画库以及 CSS 动画。以下是几种常见的实现方法。

使用 Vue 内置过渡组件

Vue 3 内置了 <transition><transition-group> 组件,用于处理元素的进入和离开动画。可以通过 CSS 或 JavaScript 钩子实现动画效果。

vue3实现动画

<template>
  <button @click="show = !show">Toggle</button>
  <transition name="fade">
    <p v-if="show">Hello, Vue 3!</p>
  </transition>
</template>

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

export default {
  setup() {
    const show = ref(true);
    return { show };
  }
};
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>

使用 CSS 动画

Vue 3 完全支持 CSS 动画,可以通过定义 CSS 关键帧动画来实现更复杂的效果。

<template>
  <button @click="animate = !animate">Toggle Animation</button>
  <div :class="{ 'bounce': animate }">Bouncing Element</div>
</template>

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

export default {
  setup() {
    const animate = ref(false);
    return { animate };
  }
};
</script>

<style>
.bounce {
  animation: bounce 1s infinite;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}
</style>

使用第三方动画库

Vue 3 可以轻松集成第三方动画库,如 GSAPAnimate.css,以实现更丰富的动画效果。

vue3实现动画

<template>
  <button @click="animateWithGSAP">Animate with GSAP</button>
  <div ref="box" class="box">GSAP Animation</div>
</template>

<script>
import { ref } from 'vue';
import gsap from 'gsap';

export default {
  setup() {
    const box = ref(null);

    const animateWithGSAP = () => {
      gsap.to(box.value, { x: 100, rotation: 360, duration: 1 });
    };

    return { box, animateWithGSAP };
  }
};
</script>

<style>
.box {
  width: 100px;
  height: 100px;
  background-color: #42b983;
}
</style>

使用 Vue 的 JavaScript 钩子

Vue 的 <transition> 组件提供了 JavaScript 钩子,可以通过这些钩子实现更复杂的动画逻辑。

<template>
  <button @click="show = !show">Toggle</button>
  <transition
    @before-enter="beforeEnter"
    @enter="enter"
    @leave="leave"
  >
    <p v-if="show">JavaScript Animation</p>
  </transition>
</template>

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

export default {
  setup() {
    const show = ref(true);

    const beforeEnter = (el) => {
      el.style.opacity = 0;
    };

    const enter = (el, done) => {
      gsap.to(el, {
        opacity: 1,
        duration: 1,
        onComplete: done
      });
    };

    const leave = (el, done) => {
      gsap.to(el, {
        opacity: 0,
        duration: 1,
        onComplete: done
      });
    };

    return { show, beforeEnter, enter, leave };
  }
};
</script>

使用 <transition-group> 实现列表动画

<transition-group> 组件用于实现列表项的动画效果,支持元素的添加、删除和移动动画。

<template>
  <button @click="addItem">Add Item</button>
  <button @click="removeItem">Remove Item</button>
  <transition-group name="list" tag="ul">
    <li v-for="item in items" :key="item">{{ item }}</li>
  </transition-group>
</template>

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

export default {
  setup() {
    const items = ref([1, 2, 3, 4, 5]);

    const addItem = () => {
      items.value.push(items.value.length + 1);
    };

    const removeItem = () => {
      items.value.pop();
    };

    return { items, addItem, removeItem };
  }
};
</script>

<style>
.list-enter-active, .list-leave-active {
  transition: all 1s;
}
.list-enter-from, .list-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
</style>

以上方法涵盖了 Vue 3 中实现动画的主要方式,可以根据具体需求选择合适的方法。

标签: 动画
分享给朋友:

相关文章

vue动画实现原理

vue动画实现原理

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

vue实现滑块动画

vue实现滑块动画

Vue 实现滑块动画的方法 使用 Vue Transition 组件 Vue 的 <transition> 组件可以轻松实现滑块动画效果。通过定义进入和离开的 CSS 过渡类,结合 v-i…

vue实现加载动画

vue实现加载动画

Vue 实现加载动画的方法 在 Vue 中实现加载动画可以通过多种方式完成,以下是一些常见的方法: 使用 CSS 动画 通过 CSS 定义动画效果,结合 Vue 的动态类绑定实现加载动画。以下是一个…

vue负责动画实现

vue负责动画实现

Vue 动画实现方法 Vue 提供了多种方式来实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件…

vue实现翻页动画

vue实现翻页动画

实现翻页动画的基本思路 在Vue中实现翻页动画通常需要结合CSS过渡或动画效果。可以通过Vue的过渡组件<transition>或<transition-group>来实现页面…

vue实现动画插件

vue实现动画插件

Vue 动画插件实现方案 Vue 提供了多种实现动画的方式,可以通过内置的 <transition> 组件或第三方插件来实现复杂的动画效果。 使用 Vue 内置 <transiti…