当前位置:首页 > VUE

vue实现小球弹入

2026-01-18 09:32:55VUE

Vue 实现小球弹入动画

使用 Vue 实现小球弹入动画可以通过 CSS 过渡或动画结合 Vue 的动态绑定特性完成。以下是几种常见方法:

方法一:使用 Vue 过渡和 CSS 动画

通过 Vue 的 <transition> 组件和 CSS 动画实现小球弹入效果。

<template>
  <button @click="showBall = !showBall">Toggle Ball</button>
  <transition name="bounce">
    <div v-if="showBall" class="ball"></div>
  </transition>
</template>

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

<style>
.ball {
  width: 50px;
  height: 50px;
  background-color: #42b983;
  border-radius: 50%;
  margin: 20px auto;
}

.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.2);
  }
  100% {
    transform: scale(1);
  }
}
</style>

方法二:结合动态样式绑定

通过 Vue 的动态样式绑定实现更灵活的控制。

<template>
  <button @click="animate">Animate Ball</button>
  <div 
    class="ball" 
    :style="{
      transform: `scale(${scale})`,
      transition: 'transform 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55)'
    }"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      scale: 0
    }
  },
  methods: {
    animate() {
      this.scale = 0;
      setTimeout(() => {
        this.scale = 1.2;
        setTimeout(() => {
          this.scale = 1;
        }, 250);
      }, 10);
    }
  }
}
</script>

方法三:使用第三方动画库

借助第三方动画库如 anime.jsgsap 实现更复杂的弹跳效果。

<template>
  <button @click="animate">Animate with GSAP</button>
  <div ref="ball" class="ball"></div>
</template>

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

export default {
  methods: {
    animate() {
      gsap.from(this.$refs.ball, {
        duration: 0.5,
        scale: 0,
        ease: "elastic.out(1, 0.5)"
      });
    }
  }
}
</script>

关键点说明

  • 使用 CSS 的 transform 属性实现缩放动画
  • 弹性效果可以通过 cubic-bezierelastic 缓动函数实现
  • 动画时序控制通过 JavaScript 的 setTimeout 或动画库完成
  • 对于复杂动画,推荐使用专业动画库如 GSAP

以上方法均可实现小球弹入效果,选择取决于项目需求和动画复杂度。CSS 方案适合简单动画,而 GSAP 等库更适合复杂交互场景。

vue实现小球弹入

标签: 小球vue
分享给朋友:

相关文章

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json)…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…