当前位置:首页 > VUE

vue实现抽奖

2026-01-07 21:51:37VUE

Vue 实现抽奖功能

基本抽奖逻辑

创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。

<template>
  <div>
    <h2>抽奖活动</h2>
    <button @click="startLottery" :disabled="isDrawing">开始抽奖</button>
    <div v-if="result">恭喜获得: {{ result }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      prizes: ['一等奖', '二等奖', '三等奖', '参与奖'],
      isDrawing: false,
      result: null
    }
  },
  methods: {
    startLottery() {
      this.isDrawing = true
      this.result = null

      setTimeout(() => {
        const randomIndex = Math.floor(Math.random() * this.prizes.length)
        this.result = this.prizes[randomIndex]
        this.isDrawing = false
      }, 2000)
    }
  }
}
</script>

动画效果增强

添加转盘动画效果,使抽奖过程更生动。使用CSS过渡和Vue的过渡组件。

<template>
  <div>
    <transition name="rotate">
      <div class="prize-wheel" :style="{ transform: `rotate(${rotation}deg)` }">
        <div v-for="(prize, index) in prizes" :key="index" class="prize-item">
          {{ prize }}
        </div>
      </div>
    </transition>
    <button @click="spinWheel">旋转转盘</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      prizes: ['奖品1', '奖品2', '奖品3', '奖品4'],
      rotation: 0,
      isSpinning: false
    }
  },
  methods: {
    spinWheel() {
      if (this.isSpinning) return

      this.isSpinning = true
      const spins = 5 // 旋转圈数
      const targetAngle = 360 * spins + Math.floor(Math.random() * 360)

      this.rotation = 0
      setTimeout(() => {
        this.rotation = targetAngle
        setTimeout(() => {
          const prizeIndex = Math.floor((targetAngle % 360) / (360 / this.prizes.length))
          alert(`恭喜获得: ${this.prizes[prizeIndex]}`)
          this.isSpinning = false
        }, 5000) // 动画持续时间
      }, 50)
    }
  }
}
</script>

<style>
.prize-wheel {
  width: 300px;
  height: 300px;
  border-radius: 50%;
  position: relative;
  transition: transform 5s cubic-bezier(0.17, 0.67, 0.12, 0.99);
  background: conic-gradient(
    red 0% 25%, 
    blue 25% 50%,
    green 50% 75%,
    yellow 75% 100%
  );
}

.prize-item {
  position: absolute;
  width: 100%;
  text-align: center;
  transform-origin: center;
  color: white;
  font-weight: bold;
}
</style>

概率控制

实现不同奖品的中奖概率控制,使用权重随机算法。

methods: {
  weightedRandom() {
    const weights = [10, 20, 30, 40] // 对应奖品的权重
    const total = weights.reduce((a, b) => a + b, 0)
    const random = Math.random() * total

    let current = 0
    for (let i = 0; i < weights.length; i++) {
      current += weights[i]
      if (random <= current) {
        return this.prizes[i]
      }
    }
    return this.prizes[0] // 默认返回第一个奖品
  }
}

服务器交互

从后端API获取奖品列表和中奖结果,增加真实性。

methods: {
  async fetchPrizes() {
    try {
      const response = await axios.get('/api/prizes')
      this.prizes = response.data
    } catch (error) {
      console.error('获取奖品失败:', error)
    }
  },

  async drawPrize() {
    try {
      const response = await axios.post('/api/draw')
      this.result = response.data.prize
    } catch (error) {
      console.error('抽奖失败:', error)
    }
  }
}

防止重复抽奖

添加抽奖次数限制,使用localStorage记录抽奖状态。

vue实现抽奖

methods: {
  checkDrawLimit() {
    const today = new Date().toDateString()
    const lastDraw = localStorage.getItem('lastDrawDate')
    const drawCount = localStorage.getItem('drawCount') || 0

    if (lastDraw === today && drawCount >= 3) {
      alert('今日抽奖次数已达上限')
      return false
    }

    return true
  },

  updateDrawRecord() {
    const today = new Date().toDateString()
    const lastDraw = localStorage.getItem('lastDrawDate')
    let drawCount = localStorage.getItem('drawCount') || 0

    if (lastDraw === today) {
      drawCount++
    } else {
      drawCount = 1
    }

    localStorage.setItem('lastDrawDate', today)
    localStorage.setItem('drawCount', drawCount)
  }
}

标签: vue
分享给朋友:

相关文章

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现导出

vue实现导出

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

vue原生实现

vue原生实现

Vue 原生实现方法 在 Vue 中实现原生功能通常指直接使用 Vue 的核心特性(如指令、响应式数据、组件等)而非依赖第三方库。以下是几种常见场景的原生实现方式: 数据绑定与响应式更新 Vue 的…

vue实现转页面

vue实现转页面

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式完成,具体取决于项目结构和需求。 使用 router-link 组件 router-link 是 Vue Router…

vue 左右滑动实现

vue 左右滑动实现

Vue 实现左右滑动功能 使用 Vue 实现左右滑动功能可以通过多种方式完成,常见的有基于原生 JavaScript 事件监听、第三方库(如 Hammer.js)或 Vue 专用插件(如 vue-to…