当前位置:首页 > VUE

vue实现红包跳动

2026-01-17 05:36:03VUE

Vue 实现红包跳动效果

红包跳动效果可以通过 CSS 动画和 Vue 的动态绑定实现。以下是具体实现方法:

动画基础实现

定义红包跳动的 CSS 关键帧动画:

vue实现红包跳动

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

.red-packet {
  width: 100px;
  height: 120px;
  background-color: #e74c3c;
  border-radius: 5px;
  position: relative;
  animation: jump 0.5s infinite alternate;
}

Vue 组件实现

创建 Vue 组件并绑定动画:

vue实现红包跳动

<template>
  <div class="red-packet-container">
    <div 
      v-for="(packet, index) in packets" 
      :key="index"
      class="red-packet"
      :style="{
        left: `${packet.position.x}px`,
        top: `${packet.position.y}px`,
        animationDelay: `${index * 0.1}s`
      }"
      @click="collectPacket(index)"
    >
      <span class="money">¥{{ packet.amount }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      packets: [
        { amount: 5, position: { x: 50, y: 100 }, collected: false },
        { amount: 10, position: { x: 150, y: 200 }, collected: false },
        { amount: 20, position: { x: 250, y: 150 }, collected: false }
      ]
    }
  },
  methods: {
    collectPacket(index) {
      if (!this.packets[index].collected) {
        this.packets[index].collected = true;
        // 这里可以添加收集动画或其他逻辑
      }
    }
  }
}
</script>

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

.red-packet {
  position: absolute;
  width: 60px;
  height: 80px;
  background-color: #e74c3c;
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
  animation: jump 0.5s infinite alternate;
}

.money {
  color: gold;
  font-weight: bold;
  font-size: 16px;
}
</style>

进阶效果优化

添加随机位置和大小:

generatePackets(count) {
  const packets = [];
  for (let i = 0; i < count; i++) {
    packets.push({
      amount: Math.floor(Math.random() * 50) + 1,
      position: {
        x: Math.random() * (window.innerWidth - 100),
        y: Math.random() * (window.innerHeight - 150)
      },
      size: Math.random() * 0.5 + 0.8,
      collected: false
    });
  }
  return packets;
}

添加收集动画:

@keyframes collect {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(3);
    opacity: 0;
  }
}

.collected {
  animation: collect 0.5s forwards !important;
}

注意事项

  1. 确保动画性能优化,使用 transformopacity 这些不会触发重排的属性
  2. 移动端适配需要考虑触摸事件
  3. 大量红包时使用虚拟滚动优化性能
  4. 可以添加音效增强用户体验

这种方法结合了 Vue 的响应式特性和 CSS 动画,可以轻松实现红包跳动效果,并且可以根据需求扩展更多功能。

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

相关文章

vue实现搜索

vue实现搜索

Vue 实现搜索功能 在 Vue 中实现搜索功能通常涉及数据绑定、事件监听和过滤逻辑。以下是几种常见的实现方式: 使用计算属性过滤列表 <template> <div>…

vue登录业务的实现

vue登录业务的实现

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

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue C…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…