当前位置:首页 > 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>

脚本部分

vue实现红包跳动

<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. 为红包添加阴影增强立体感

    vue实现红包跳动

    .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. 添加随机跳动效果

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

注意事项

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

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

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

相关文章

vue实现点击样式

vue实现点击样式

Vue 实现点击样式的方法 在 Vue 中实现点击样式可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-bind:class 动态绑定类名 通过数据驱动的方式动态切换类名,结合 CS…

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…