当前位置:首页 > 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实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如a…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装 scre…