当前位置:首页 > VUE

vue实现人员滚动抽奖

2026-02-22 11:39:35VUE

Vue实现人员滚动抽奖

核心思路

通过Vue的数据绑定和动画特性,结合JavaScript定时器实现人员名单的滚动效果,最终随机停止并显示中奖结果。

实现步骤

准备数据与基础模板

vue实现人员滚动抽奖

<template>
  <div class="lottery-container">
    <div class="lottery-display">{{ currentName }}</div>
    <button @click="startLottery" :disabled="isRolling">开始抽奖</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      names: ['张三', '李四', '王五', '赵六', '钱七', '孙八'],
      currentName: '准备开始',
      isRolling: false,
      timer: null,
      rollSpeed: 100
    }
  }
}
</script>

实现滚动逻辑

methods: {
  startLottery() {
    this.isRolling = true
    this.currentName = '抽奖中...'

    this.timer = setInterval(() => {
      const randomIndex = Math.floor(Math.random() * this.names.length)
      this.currentName = this.names[randomIndex]
    }, this.rollSpeed)

    setTimeout(() => {
      clearInterval(this.timer)
      this.isRolling = false
      this.finalResult()
    }, 3000)
  },

  finalResult() {
    const winnerIndex = Math.floor(Math.random() * this.names.length)
    this.currentName = `中奖者: ${this.names[winnerIndex]}`
  }
}

添加CSS动画效果

vue实现人员滚动抽奖

<style scoped>
.lottery-container {
  text-align: center;
  margin-top: 50px;
}

.lottery-display {
  font-size: 24px;
  height: 40px;
  margin: 20px 0;
  transition: all 0.3s;
}

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

button:disabled {
  background-color: #cccccc;
}
</style>

高级优化方案

增加加速度效果

// 修改startLottery方法
startLottery() {
  this.isRolling = true
  let duration = 3000
  let startTime = Date.now()

  const roll = () => {
    if (Date.now() - startTime >= duration) {
      clearInterval(this.timer)
      this.finalResult()
      this.isRolling = false
      return
    }

    const elapsed = Date.now() - startTime
    const progress = elapsed / duration
    this.rollSpeed = 100 + (500 * (1 - progress))

    clearInterval(this.timer)
    this.timer = setInterval(() => {
      const randomIndex = Math.floor(Math.random() * this.names.length)
      this.currentName = this.names[randomIndex]
    }, this.rollSpeed)

    requestAnimationFrame(roll)
  }

  roll()
}

添加音效支持

<audio ref="rollSound" src="rolling-sound.mp3"></audio>
<audio ref="winSound" src="win-sound.mp3"></audio>

<!-- 在methods中添加 -->
playSound(refName) {
  this.$refs[refName].currentTime = 0
  this.$refs[refName].play()
}

注意事项

  • 移动端适配可能需要调整字体大小和按钮尺寸
  • 长时间抽奖应考虑使用Web Worker防止主线程阻塞
  • 大量数据时应采用虚拟滚动技术优化性能
  • 中奖逻辑可根据需求改为权重算法或排除已中奖人员

标签: 人员vue
分享给朋友:

相关文章

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…