当前位置:首页 > VUE

vue动画实现代码

2026-02-22 16:16:19VUE

Vue 动画实现代码

Vue 提供了内置的过渡和动画系统,主要通过 <transition><transition-group> 组件实现。以下是一些常见的动画实现方式:

基础过渡动画

通过 CSS 过渡实现元素的淡入淡出效果:

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

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

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

使用 CSS 动画

通过 CSS 关键帧动画实现更复杂的动画效果:

<template>
  <button @click="show = !show">Toggle</button>
  <transition name="bounce">
    <p v-if="show">Bouncing Text</p>
  </transition>
</template>

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

<style>
.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.25);
  }
  100% {
    transform: scale(1);
  }
}
</style>

列表过渡

使用 <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.id">
      {{ item.text }}
    </li>
  </transition-group>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' }
      ],
      nextId: 4
    }
  },
  methods: {
    addItem() {
      this.items.push({
        id: this.nextId++,
        text: 'Item ' + this.nextId
      })
    },
    removeItem() {
      this.items.pop()
    }
  }
}
</script>

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

JavaScript 钩子动画

通过 JavaScript 钩子实现更精细的动画控制:

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

<script>
export default {
  data() {
    return {
      show: false
    }
  },
  methods: {
    beforeEnter(el) {
      el.style.opacity = 0
      el.style.transform = 'scale(0)'
    },
    enter(el, done) {
      const animation = el.animate([
        { opacity: 0, transform: 'scale(0)' },
        { opacity: 1, transform: 'scale(1.2)' },
        { opacity: 1, transform: 'scale(1)' }
      ], {
        duration: 1000
      })
      animation.onfinish = done
    },
    leave(el, done) {
      const animation = el.animate([
        { opacity: 1, transform: 'scale(1)' },
        { opacity: 0, transform: 'scale(0)' }
      ], {
        duration: 800
      })
      animation.onfinish = done
    }
  }
}
</script>

第三方动画库集成

可以与 Animate.css 等第三方动画库集成:

<template>
  <button @click="show = !show">Toggle</button>
  <transition
    enter-active-class="animate__animated animate__bounceIn"
    leave-active-class="animate__animated animate__bounceOut"
  >
    <p v-if="show">Animate.css Animation</p>
  </transition>
</template>

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

<!-- 引入 Animate.css -->
<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>

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

vue动画实现代码

标签: 代码动画
分享给朋友:

相关文章

vue实现gif动画

vue实现gif动画

Vue 中实现 GIF 动画的方法 在 Vue 中实现 GIF 动画可以通过多种方式完成,具体取决于需求和使用场景。以下是几种常见的方法: 直接使用 <img> 标签加载 GIF 最简单…

js实现动画

js实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

vue实现代码

vue实现代码

以下是Vue实现常见功能的代码示例,涵盖基础语法、组件通信、状态管理等核心内容: 基础模板语法 <template> <div> <h1>{{ mes…

vue动画实现方式

vue动画实现方式

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

vue实现加载动画

vue实现加载动画

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

vue中实现动画

vue中实现动画

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