当前位置:首页 > VUE

vue实现半场动画

2026-01-17 12:59:07VUE

Vue 实现半场动画

在 Vue 中实现半场动画通常指的是动画执行到一半时停止或改变状态。可以通过 Vue 的过渡系统或第三方动画库(如 GSAP)实现。

vue实现半场动画

使用 Vue 过渡

Vue 提供了 <transition><transition-group> 组件,结合 CSS 过渡或动画类名,可以控制动画的中间状态。

vue实现半场动画

<template>
  <div>
    <button @click="toggle">Toggle Animation</button>
    <transition name="halfway">
      <div v-if="show" class="box"></div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false
    };
  },
  methods: {
    toggle() {
      this.show = !this.show;
    }
  }
};
</script>

<style>
.halfway-enter-active {
  animation: slide-in 0.5s;
}
.halfway-leave-active {
  animation: slide-in 0.5s reverse;
}
@keyframes slide-in {
  0% {
    transform: translateX(-100%);
  }
  50% {
    transform: translateX(0);
    opacity: 1;
  }
  100% {
    transform: translateX(100%);
    opacity: 0;
  }
}
.box {
  width: 100px;
  height: 100px;
  background: #42b983;
}
</style>

使用 GSAP 控制动画

GSAP 提供了更精细的动画控制,可以通过 pause()seek() 方法实现半场动画。

<template>
  <div>
    <button @click="startAnimation">Start Animation</button>
    <button @click="pauseAtHalfway">Pause at Halfway</button>
    <div ref="box" class="box"></div>
  </div>
</template>

<script>
import { gsap } from "gsap";

export default {
  methods: {
    startAnimation() {
      gsap.to(this.$refs.box, {
        x: 200,
        duration: 2,
        opacity: 0
      });
    },
    pauseAtHalfway() {
      const animation = gsap.getTweensOf(this.$refs.box)[0];
      if (animation) {
        animation.pause();
        animation.seek(1); // 1 秒时暂停(假设总时长为 2 秒)
      }
    }
  }
};
</script>

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

使用 JavaScript 钩子

Vue 的过渡系统提供了 JavaScript 钩子(如 @enter@leave),可以在动画过程中插入自定义逻辑。

<template>
  <div>
    <button @click="toggle">Toggle Animation</button>
    <transition
      @enter="enter"
      @leave="leave"
      :css="false"
    >
      <div v-if="show" class="box"></div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false
    };
  },
  methods: {
    toggle() {
      this.show = !this.show;
    },
    enter(el, done) {
      const animation = el.animate(
        [
          { transform: "translateX(-100%)", opacity: 0 },
          { transform: "translateX(0)", opacity: 1 },
          { transform: "translateX(100%)", opacity: 0 }
        ],
        {
          duration: 1000
        }
      );
      animation.onfinish = done;
      // 半场停止
      setTimeout(() => {
        animation.pause();
      }, 500);
    },
    leave(el, done) {
      const animation = el.animate(
        [
          { transform: "translateX(0)", opacity: 1 },
          { transform: "translateX(100%)", opacity: 0 }
        ],
        {
          duration: 1000
        }
      );
      animation.onfinish = done;
    }
  }
};
</script>

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

总结

  • Vue 过渡系统适合简单的半场动画,通过 CSS 或 JavaScript 钩子控制。
  • GSAP 提供更强大的动画控制,适合复杂场景。
  • JavaScript 钩子可以在动画过程中插入自定义逻辑,实现精确控制。

标签: 半场动画
分享给朋友:

相关文章

vue中实现动画

vue中实现动画

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

点击实现vue动画

点击实现vue动画

在Vue中实现动画效果可以通过内置的<transition>和<transition-group>组件结合CSS或JavaScript钩子完成。以下是具体方法: 使用CSS过…

vue实现轮播图动画

vue实现轮播图动画

Vue 轮播图动画实现方案 基础实现(基于CSS过渡) 使用Vue的<transition>组件结合CSS实现基础轮播动画: <template> <div cla…

vue不点击实现动画

vue不点击实现动画

在Vue中实现不依赖点击触发的动画,可以通过多种方式实现,例如利用生命周期钩子、条件渲染、CSS动画或第三方库。以下是几种常见方法: 使用Vue的过渡和动画系统 Vue内置的<transiti…

vue实现滚动动画

vue实现滚动动画

Vue 实现滚动动画的方法 使用 CSS Transition 和 Vue 指令 在 Vue 中可以通过结合 CSS Transition 和自定义指令实现滚动动画效果。定义一个指令监听元素的滚动位置…

vue中实现动画效果

vue中实现动画效果

在Vue中实现动画效果可以通过多种方式,包括内置组件、CSS动画库或JavaScript动画库。以下是几种常见方法: 使用Vue内置的过渡组件 Vue提供了<transition>和&l…