当前位置:首页 > VUE

vue实现停止动画

2026-03-10 02:29:39VUE

停止 CSS 动画

在 Vue 中停止 CSS 动画可以通过移除或修改元素的 class 或内联样式来实现。CSS 动画通常通过 @keyframestransition 实现,移除相关类名即可停止动画。

<template>
  <div :class="{ 'animate': isAnimating }" @click="toggleAnimation">
    点击停止动画
  </div>
</template>

<script>
export default {
  data() {
    return {
      isAnimating: true
    };
  },
  methods: {
    toggleAnimation() {
      this.isAnimating = !this.isAnimating;
    }
  }
};
</script>

<style>
.animate {
  animation: spin 2s infinite linear;
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>

停止 JavaScript 动画

对于使用 JavaScript 实现的动画(如 requestAnimationFrame 或第三方库如 GSAP),需要手动取消动画帧或调用停止方法。

vue实现停止动画

<template>
  <div ref="box" @click="stopAnimation">点击停止动画</div>
</template>

<script>
export default {
  data() {
    return {
      animationId: null,
      position: 0
    };
  },
  mounted() {
    this.startAnimation();
  },
  methods: {
    startAnimation() {
      const animate = () => {
        this.position += 1;
        this.$refs.box.style.transform = `translateX(${this.position}px)`;
        this.animationId = requestAnimationFrame(animate);
      };
      this.animationId = requestAnimationFrame(animate);
    },
    stopAnimation() {
      if (this.animationId) {
        cancelAnimationFrame(this.animationId);
        this.animationId = null;
      }
    }
  }
};
</script>

停止第三方动画库

如果使用 GSAP、Anime.js 等第三方动画库,通常需要调用其提供的停止或销毁方法。

vue实现停止动画

<template>
  <div ref="target" @click="stopAnimation">点击停止动画</div>
</template>

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

export default {
  data() {
    return {
      tween: null
    };
  },
  mounted() {
    this.tween = gsap.to(this.$refs.target, {
      x: 100,
      duration: 2,
      repeat: -1
    });
  },
  methods: {
    stopAnimation() {
      if (this.tween) {
        this.tween.kill();
        this.tween = null;
      }
    }
  }
};
</script>

停止 Vue 过渡动画

Vue 的 <transition> 组件可以通过绑定 v-ifv-show 动态控制动画的启停。

<template>
  <button @click="show = !show">切换动画</button>
  <transition name="fade">
    <div v-if="show">内容</div>
  </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>

动态绑定样式停止动画

通过动态绑定内联样式或类名,可以灵活控制动画的播放状态。

<template>
  <div 
    :style="{ animationPlayState: isPaused ? 'paused' : 'running' }" 
    class="spin" 
    @click="isPaused = !isPaused"
  >
    点击暂停/播放
  </div>
</template>

<script>
export default {
  data() {
    return {
      isPaused: false
    };
  }
};
</script>

<style>
.spin {
  animation: spin 2s infinite linear;
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>

标签: 动画vue
分享给朋友:

相关文章

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录组…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…