当前位置:首页 > 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中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起: &l…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…

vue实现卡片

vue实现卡片

Vue 实现卡片组件的方法 使用 Vue 实现卡片组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 在 Vue 单文件组件中,可以通过模板和样式直接实现卡片效果…