当前位置:首页 > VUE

vue 实现转盘抽奖

2026-01-19 05:37:05VUE

vue 实现转盘抽奖

实现思路

使用Vue结合CSS动画和随机数生成实现转盘抽奖功能。核心是通过CSS控制转盘旋转,通过JavaScript计算旋转角度和结果判定。

vue 实现转盘抽奖

基础HTML结构

<template>
  <div class="lottery-container">
    <div class="wheel" :style="wheelStyle">
      <div class="wheel-item" v-for="(item, index) in prizes" :key="index" 
           :style="getItemStyle(index)">
        {{ item.name }}
      </div>
    </div>
    <button @click="startLottery" :disabled="isRotating">开始抽奖</button>
  </div>
</template>

CSS样式

.wheel {
  width: 300px;
  height: 300px;
  border-radius: 50%;
  position: relative;
  transition: transform 4s cubic-bezier(0.17, 0.67, 0.12, 0.99);
}

.wheel-item {
  position: absolute;
  width: 50%;
  height: 50%;
  transform-origin: 100% 100%;
  left: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #fff;
}

.lottery-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

JavaScript逻辑

export default {
  data() {
    return {
      prizes: [
        { name: '奖品1', color: '#FF5252' },
        { name: '奖品2', color: '#FF4081' },
        { name: '奖品3', color: '#E040FB' },
        { name: '奖品4', color: '#7C4DFF' },
        { name: '奖品5', color: '#536DFE' },
        { name: '奖品6', color: '#448AFF' }
      ],
      rotation: 0,
      isRotating: false
    }
  },
  computed: {
    wheelStyle() {
      return {
        transform: `rotate(${this.rotation}deg)`,
        background: 'conic-gradient(' + 
          this.prizes.map((prize, i) => 
            `${prize.color} ${i * (360/this.prizes.length)}deg ${(i+1) * (360/this.prizes.length)}deg`
          ).join(', ') + ')'
      }
    }
  },
  methods: {
    getItemStyle(index) {
      const angle = 360 / this.prizes.length
      return {
        transform: `rotate(${angle * index + angle/2}deg)`,
        backgroundColor: this.prizes[index].color
      }
    },
    startLottery() {
      if (this.isRotating) return

      this.isRotating = true
      const rounds = 5 // 旋转圈数
      const randomIndex = Math.floor(Math.random() * this.prizes.length)
      const targetAngle = 360 * rounds + (360 - (360 / this.prizes.length) * randomIndex)

      this.rotation = this.rotation % 360
      this.rotation += targetAngle

      setTimeout(() => {
        this.isRotating = false
        alert(`恭喜获得: ${this.prizes[randomIndex].name}`)
      }, 4000)
    }
  }
}

优化建议

  1. 添加指针元素固定在转盘中心
  2. 增加音效增强体验
  3. 奖品数据可配置化,支持从后端获取
  4. 添加旋转过程中的防抖处理
  5. 支持自定义转盘尺寸和颜色

实现原理

旋转角度计算公式: $$ \text{总角度} = 360 \times \text{圈数} + (360 - \frac{360}{\text{奖品数量}} \times \text{随机索引}) $$

通过CSS的transition实现平滑旋转效果,cubic-bezier函数控制旋转速度曲线,使旋转过程有缓动效果更自然。旋转结束后根据最终角度计算落在哪个奖品区域。

标签: 转盘vue
分享给朋友:

相关文章

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue实现尺子

vue实现尺子

Vue 实现尺子组件 实现一个尺子组件可以通过 Canvas 或纯 CSS 完成。以下是两种常见实现方式: 使用 Canvas 绘制尺子 通过 Canvas 动态绘制刻度线和数字,适合需要高精度或动…

vue实现甘特图

vue实现甘特图

使用 Vue 实现甘特图 基于开源库(如 vue-ganttastic) 安装依赖库: npm install vue-ganttastic 示例代码: <template> &l…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…