当前位置:首页 > VUE

vue实现红包跳动

2026-02-17 22:00:37VUE

Vue 实现红包跳动效果

要实现红包跳动效果,可以使用 CSS 动画结合 Vue 的动态绑定功能。以下是一个完整的实现方案:

vue实现红包跳动

创建红包组件

<template>
  <div class="red-packet" :style="animationStyle" @click="handleClick">
    <div class="envelope"></div>
    <div class="money">¥</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isJumping: false,
      jumpHeight: 0
    }
  },
  computed: {
    animationStyle() {
      return {
        transform: this.isJumping ? `translateY(-${this.jumpHeight}px)` : '',
        transition: this.isJumping ? 'transform 0.3s ease-in-out' : ''
      }
    }
  },
  methods: {
    startJumping() {
      this.jumpHeight = Math.random() * 20 + 10
      this.isJumping = true
      setTimeout(() => {
        this.isJumping = false
      }, 300)
    },
    handleClick() {
      this.startJumping()
      // 可以在这里添加点击后的逻辑,如打开红包等
    }
  },
  mounted() {
    setInterval(() => {
      this.startJumping()
    }, 2000)
  }
}
</script>

<style scoped>
.red-packet {
  position: relative;
  width: 100px;
  height: 120px;
  cursor: pointer;
}

.envelope {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #e74c3c;
  border-radius: 5px;
  clip-path: polygon(
    0 0,
    100% 0,
    100% 85%,
    50% 100%,
    0 85%
  );
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.money {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
  font-weight: bold;
  color: #f1c40f;
}
</style>

增强跳动效果

如果需要更复杂的跳动效果,可以使用 CSS 关键帧动画:

vue实现红包跳动

@keyframes jump {
  0% { transform: translateY(0); }
  50% { transform: translateY(-30px); }
  100% { transform: translateY(0); }
}

.red-packet {
  animation: jump 1s infinite alternate;
}

多个红包随机跳动

对于多个红包随机跳动的场景:

<template>
  <div class="red-packets-container">
    <red-packet 
      v-for="(packet, index) in packets" 
      :key="index"
      :style="{
        left: `${packet.left}%`,
        top: `${packet.top}%`,
        animationDelay: `${packet.delay}s`
      }"
    />
  </div>
</template>

<script>
import RedPacket from './RedPacket.vue'

export default {
  components: { RedPacket },
  data() {
    return {
      packets: Array(5).fill().map(() => ({
        left: Math.random() * 80 + 10,
        top: Math.random() * 60 + 20,
        delay: Math.random() * 2
      }))
    }
  }
}
</script>

<style>
.red-packets-container {
  position: relative;
  width: 100%;
  height: 500px;
}

.red-packet {
  position: absolute;
  animation: jump 1s infinite alternate;
}
</style>

点击特效

添加点击红包时的爆炸效果:

<template>
  <div>
    <div 
      class="red-packet" 
      @click="explode"
      v-if="!exploded"
    >
      <!-- 红包内容 -->
    </div>
    <div class="explosion" v-if="exploded">
      <div 
        v-for="(particle, index) in particles" 
        :key="index"
        class="particle"
        :style="particle.style"
      ></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      exploded: false,
      particles: Array(20).fill().map(() => ({
        style: {
          left: '50%',
          top: '50%',
          backgroundColor: this.getRandomColor(),
          transform: `rotate(${Math.random() * 360}deg)`,
          animation: `fly ${Math.random() * 1 + 0.5}s forwards`
        }
      }))
    }
  },
  methods: {
    explode() {
      this.exploded = true
      setTimeout(() => {
        this.exploded = false
      }, 1000)
    },
    getRandomColor() {
      const colors = ['#f1c40f', '#e74c3c', '#e67e22', '#2ecc71']
      return colors[Math.floor(Math.random() * colors.length)]
    }
  }
}
</script>

<style>
.explosion {
  position: absolute;
  width: 100%;
  height: 100%;
}

.particle {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

@keyframes fly {
  0% {
    transform: translate(0, 0) rotate(0);
    opacity: 1;
  }
  100% {
    transform: translate(
      calc(var(--x) * 100px), 
      calc(var(--y) * 100px)
    );
    opacity: 0;
  }
}

.particle:nth-child(odd) {
  --x: 0.5;
  --y: -0.7;
}
.particle:nth-child(even) {
  --x: -0.4;
  --y: -0.5;
}
/* 更多粒子方向变化 */
</style>

这些方案可以根据实际需求进行调整和组合,实现不同风格的红包跳动效果。

标签: 红包vue
分享给朋友:

相关文章

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue 实现聊天

vue 实现聊天

使用 Vue 实现聊天功能 创建 Vue 项目并安装依赖 确保已安装 Vue CLI,通过以下命令创建新项目: vue create chat-app 进入项目目录后,安装必要的依赖(如 Socke…

vue实现座位

vue实现座位

Vue实现座位布局 使用Vue实现座位布局可以通过组件化和响应式数据管理来简化开发流程。以下是一个完整的实现方案: 数据准备 定义座位数据结构,通常使用二维数组表示行列关系: data() {…