当前位置:首页 > VUE

vue2实现抽奖

2026-02-22 17:42:59VUE

Vue2 抽奖功能实现

数据准备与初始化

在Vue实例的data中定义奖品列表和抽奖状态

data() {
  return {
    prizes: [
      { id: 1, name: '一等奖' },
      { id: 2, name: '二等奖' },
      { id: 3, name: '三等奖' },
      { id: 4, name: '参与奖' }
    ],
    currentIndex: -1,
    isRolling: false,
    rollSpeed: 100,
    result: null
  }
}

抽奖动画实现

使用定时器实现转盘效果,通过不断改变currentIndex来高亮不同奖品

vue2实现抽奖

methods: {
  startRoll() {
    if (this.isRolling) return

    this.isRolling = true
    this.result = null
    let rollCount = 0
    const totalRolls = 30 + Math.floor(Math.random() * 20)

    const roll = () => {
      rollCount++
      this.currentIndex = (this.currentIndex + 1) % this.prizes.length

      if (rollCount < totalRolls) {
        setTimeout(roll, this.rollSpeed)
      } else {
        this.stopRoll()
      }

      // 逐渐减速
      if (rollCount > totalRolls * 0.7) {
        this.rollSpeed += 20
      }
    }

    roll()
  }
}

停止抽奖逻辑

确定最终获奖结果并重置状态

methods: {
  stopRoll() {
    this.isRolling = false
    this.rollSpeed = 100
    // 随机确定获奖结果
    this.result = this.prizes[Math.floor(Math.random() * this.prizes.length)]
    // 高亮显示获奖项
    this.currentIndex = this.prizes.findIndex(
      item => item.id === this.result.id
    )
  }
}

模板部分

使用v-for渲染奖品列表,通过动态class实现高亮效果

vue2实现抽奖

<div class="lottery-container">
  <div 
    v-for="(prize, index) in prizes" 
    :key="prize.id"
    :class="['prize-item', { active: currentIndex === index }]"
  >
    {{ prize.name }}
  </div>
  <button @click="startRoll" :disabled="isRolling">
    {{ isRolling ? '抽奖中...' : '开始抽奖' }}
  </button>
  <div v-if="result" class="result">
    恭喜获得: {{ result.name }}
  </div>
</div>

样式设计

添加基础样式和动画效果

.lottery-container {
  display: flex;
  flex-wrap: wrap;
  max-width: 400px;
  margin: 0 auto;
}

.prize-item {
  width: 45%;
  margin: 2.5%;
  padding: 20px;
  text-align: center;
  background: #f0f0f0;
  border-radius: 8px;
  transition: all 0.3s;
}

.prize-item.active {
  background: #ffeb3b;
  transform: scale(1.05);
  box-shadow: 0 0 10px rgba(0,0,0,0.2);
}

button {
  margin: 20px auto;
  padding: 10px 20px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:disabled {
  background: #cccccc;
}

进阶优化

可考虑添加以下增强功能:

  • 增加音效(开始/停止音效)
  • 实现真实转盘UI而非列表布局
  • 添加中奖概率控制(不同奖项不同概率)
  • 增加抽奖次数限制功能
  • 使用transition-group实现更流畅的动画效果

分享给朋友:

相关文章

vue实现路由历史

vue实现路由历史

vue实现路由历史 在Vue中实现路由历史管理,通常通过Vue Router的history模式完成。以下是具体实现方法和注意事项: 配置history模式 在Vue Router初始化时,设置m…

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="bu…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类…