当前位置:首页 > VUE

vue实现投币动画效果

2026-01-20 23:00:10VUE

实现投币动画效果的方法

在Vue中实现投币动画效果,可以通过CSS动画和Vue的过渡系统结合完成。以下是几种常见的实现方式:

vue实现投币动画效果

使用CSS动画和Vue过渡

定义一个硬币组件,通过CSS动画控制硬币的旋转和下落的视觉效果。

vue实现投币动画效果

<template>
  <div class="coin-container">
    <transition name="coin-drop" @before-enter="beforeEnter" @enter="enter">
      <div v-if="showCoin" class="coin" :style="coinStyle"></div>
    </transition>
    <button @click="throwCoin">投币</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showCoin: false,
      coinStyle: {}
    }
  },
  methods: {
    throwCoin() {
      this.showCoin = true
      setTimeout(() => {
        this.showCoin = false
      }, 2000)
    },
    beforeEnter(el) {
      el.style.transform = 'translateY(-100px) rotate(0deg)'
      el.style.opacity = '0'
    },
    enter(el, done) {
      let rotate = 0
      const rotateInterval = setInterval(() => {
        rotate += 30
        el.style.transform = `translateY(${Math.min(100, rotate/3)}px) rotate(${rotate}deg)`
        el.style.opacity = '1'
        if (rotate >= 360) {
          clearInterval(rotateInterval)
          done()
        }
      }, 20)
    }
  }
}
</script>

<style>
.coin-container {
  position: relative;
  height: 200px;
}

.coin {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: gold;
  position: absolute;
  top: 0;
  left: 50%;
  margin-left: -20px;
  box-shadow: 0 0 10px gold;
}

.coin-drop-enter-active {
  transition: all 1s ease;
}
</style>

使用GSAP动画库

对于更复杂的动画效果,可以使用GSAP动画库来实现更流畅的投币动画。

<template>
  <div class="coin-game">
    <div ref="coin" class="coin" v-show="isCoinVisible"></div>
    <button @click="tossCoin">投币</button>
  </div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  data() {
    return {
      isCoinVisible: false
    }
  },
  methods: {
    tossCoin() {
      this.isCoinVisible = true
      const coin = this.$refs.coin

      gsap.fromTo(coin, 
        { y: -100, rotation: 0, opacity: 0 },
        { 
          y: 100, 
          rotation: 720,
          opacity: 1,
          duration: 1.5,
          ease: "bounce.out",
          onComplete: () => {
            this.isCoinVisible = false
          }
        }
      )
    }
  }
}
</script>

<style>
.coin-game {
  position: relative;
  height: 200px;
}

.coin {
  width: 40px;
  height: 40px;
  background: radial-gradient(circle at 30% 30%, #FFD700, #D4AF37);
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 50%;
  margin-left: -20px;
  box-shadow: 0 0 10px gold;
}
</style>

多个硬币同时投掷的效果

如果需要实现多个硬币同时投掷的效果,可以使用Vue的动态组件和列表渲染。

<template>
  <div class="coin-machine">
    <transition-group name="coins" tag="div" class="coin-area">
      <div 
        v-for="(coin, index) in coins" 
        :key="index"
        class="coin"
        :style="coin.style"
      ></div>
    </transition-group>
    <button @click="addCoins(3)">投3个币</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      coins: []
    }
  },
  methods: {
    addCoins(count) {
      for (let i = 0; i < count; i++) {
        const coin = {
          id: Date.now() + i,
          style: {
            left: `${30 + i * 20}%`,
            animation: `coin-drop ${0.5 + i * 0.2}s ease-out forwards`
          }
        }
        this.coins.push(coin)

        setTimeout(() => {
          this.coins = this.coins.filter(c => c.id !== coin.id)
        }, 2000)
      }
    }
  }
}
</script>

<style>
.coin-machine {
  position: relative;
  height: 300px;
}

.coin-area {
  position: relative;
  height: 100%;
}

.coin {
  width: 30px;
  height: 30px;
  background: gold;
  border-radius: 50%;
  position: absolute;
  top: 0;
  box-shadow: 0 0 8px gold;
}

@keyframes coin-drop {
  0% {
    transform: translateY(-100px) rotate(0deg);
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    transform: translateY(200px) rotate(360deg);
    opacity: 0;
  }
}
</style>

这些方法提供了从简单到复杂的投币动画实现方案,可以根据项目需求选择合适的实现方式。CSS动画适合简单效果,GSAP适合需要精细控制的复杂动画,而transition-group则适合处理多个硬币同时动画的场景。

标签: 效果动画
分享给朋友:

相关文章

css3动画在线制作

css3动画在线制作

CSS3 动画在线制作工具 以下是一些常用的在线工具和平台,可以帮助快速生成 CSS3 动画代码,无需手动编写: Animista 网址:https://animista.net/ 特点:提供预设的…

vue动画实现原理

vue动画实现原理

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

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

h5实现左右滚动效果

h5实现左右滚动效果

使用CSS实现横向滚动 在HTML5中实现左右滚动效果,可以通过CSS的overflow-x属性配合white-space或flex布局实现。以下是一个基础示例: <div class="s…

h5页面滑动效果实现

h5页面滑动效果实现

实现H5页面滑动效果的方法 H5页面滑动效果可以通过多种方式实现,包括原生JavaScript、CSS动画或第三方库。以下是几种常见的方法: 使用CSS Scroll Snap CSS Scroll…

vue实现路由动画

vue实现路由动画

路由动画的实现方法 在Vue中实现路由动画通常结合<transition>组件和Vue Router的导航守卫。以下是几种常见实现方式: 基础过渡动画 <template>…