当前位置:首页 > VUE

vue实现红包跳动

2026-03-29 22:22:24VUE

实现红包跳动效果的思路

使用Vue结合CSS动画可以实现红包跳动效果,核心是通过关键帧动画控制红包元素的上下移动和旋转。

关键代码实现

模板部分

<template>
  <div class="red-packet-container">
    <div 
      class="red-packet" 
      :style="{ animationDelay: `${index * 0.2}s` }"
      v-for="(packet, index) in packets" 
      :key="index"
      @click="handleClick(packet)"
    >
      <div class="envelope"></div>
      <div class="money">{{ packet.amount }}元</div>
    </div>
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      packets: [
        { id: 1, amount: 5 },
        { id: 2, amount: 10 },
        { id: 3, amount: 20 }
      ]
    }
  },
  methods: {
    handleClick(packet) {
      console.log('领取红包:', packet.amount)
    }
  }
}
</script>

样式部分

<style>
.red-packet-container {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding: 50px;
}

.red-packet {
  position: relative;
  width: 100px;
  height: 120px;
  animation: jump 1s infinite alternate;
  cursor: pointer;
}

.envelope {
  width: 100%;
  height: 100%;
  background-color: #e74c3c;
  border-radius: 5px;
  position: relative;
}

.money {
  position: absolute;
  bottom: 10px;
  left: 0;
  right: 0;
  text-align: center;
  color: #f1c40f;
  font-weight: bold;
}

@keyframes jump {
  0% {
    transform: translateY(0) rotate(-5deg);
  }
  50% {
    transform: translateY(-20px) rotate(0deg);
  }
  100% {
    transform: translateY(0) rotate(5deg);
  }
}
</style>

效果优化建议

  1. 为红包添加阴影增强立体感

    .red-packet {
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    }
  2. 点击时添加缩放动画

    .red-packet:active {
    transform: scale(0.9);
    transition: transform 0.1s;
    }
  3. 添加随机跳动效果

    vue实现红包跳动

    computed: {
    randomDelays() {
     return this.packets.map(() => Math.random() * 0.5 + 's')
    }
    }

注意事项

  • 动画性能优化:使用transform代替top/left等属性
  • 移动端适配:考虑添加touch事件支持
  • 可访问性:为红包添加ARIA标签

以上代码实现了基本的红包跳动效果,可根据实际需求调整动画参数和样式细节。

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

相关文章

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue实现引导

vue实现引导

Vue 实现引导功能的方法 使用第三方库 推荐使用 driver.js 或 intro.js 这类专门为 Vue 设计的引导库。这些库提供了完整的 API 和样式,能够快速实现引导功能。 安装 dr…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…