当前位置:首页 > 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 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-lig…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.de…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…