当前位置: 首页 > article >正文

使用纯CSS和JavaScript来实现一个转盘抽奖效果(设置机率)

使用纯CSS和JavaScript来实现一个转盘抽奖效果。我们可以设定每个奖项的概率,并使用加权随机算法来决定最终的中奖奖项。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>转盘抽奖</title>
    <style>
      body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        margin: 0;
        background-color: #f7f7f7;
      }

      .container {
        position: relative;
        width: 300px;
        height: 300px;
      }

      /* 轮盘容器 */
      .wheel {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 5px solid #333;
        position: relative;
        overflow: hidden;
        transform: rotate(-36deg);
        transition: transform 5s ease-out;
      }

      /* 使用 clip-path 切分成10个扇形,并设置z-index */
      .wheel div {
        position: absolute;
        width: 100%;
        height: 100%;
        clip-path: polygon(50% 50%, 87% -3%, 100% 33%);
        transform-origin: 50% 50%;
        z-index: 1;
      }

      /* 设置每个扇形区域的颜色和旋转角度 */
      .wheel div:nth-child(1) {
        background-color: #f39c12;
        transform: rotate(0deg);
      }
      .wheel div:nth-child(2) {
        background-color: #e74c3c;
        transform: rotate(36deg);
      }
      .wheel div:nth-child(3) {
        background-color: #8e44ad;
        transform: rotate(72deg);
      }
      .wheel div:nth-child(4) {
        background-color: #3498db;
        transform: rotate(108deg);
      }
      .wheel div:nth-child(5) {
        background-color: #1abc9c;
        transform: rotate(144deg);
      }
      .wheel div:nth-child(6) {
        background-color: #27ae60;
        transform: rotate(180deg);
      }
      .wheel div:nth-child(7) {
        background-color: #f1c40f;
        transform: rotate(216deg);
      }
      .wheel div:nth-child(8) {
        background-color: #2ecc71;
        transform: rotate(252deg);
      }
      .wheel div:nth-child(9) {
        background-color: #e67e22;
        transform: rotate(288deg);
      }
      .wheel div:nth-child(10) {
        background-color: #c0392b;
        transform: rotate(324deg);
      }

      /* 奖项名称的显示 */
      .wheel div span {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(150%, -380%) rotate(-45deg);
        font-size: 12px;
        color: #fff;
        text-align: center;
      }

      /* 指针样式 */
      .pointer {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 0;
        height: 0;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        border-bottom: 130px solid #333;
        transform: translate(4%, -96%);
        transform-origin: bottom center; /* 将旋转中心点移到指针底部 */
        z-index: 10;
      }
      button {
        width: 100px;
        height: 32px;
        border-radius: 20px;
        margin-top: 40px;
      }
    </style>
  </head>
  <body>
    <div style="display: flex; flex-direction: column; align-items: center">
      <div class="container">
        <div class="wheel" id="wheel">
          <div><span>一等奖</span></div>
          <div><span>二等奖</span></div>
          <div><span>三等奖</span></div>
          <div><span>四等奖</span></div>
          <div><span>五等奖</span></div>
          <div><span>六等奖</span></div>
          <div><span>七等奖</span></div>
          <div><span>八等奖</span></div>
          <div><span>九等奖</span></div>
          <div><span>十等奖</span></div>
        </div>
        <div class="pointer"></div>
      </div>

      <button onclick="spinPointer()">开始抽奖</button>
    </div>
    <script>
      let isSpinning = false
      const pointer = document.querySelector('.pointer')
      // 定义每个奖项及其对应的中奖概率 (权重)
      const prizes = [
        { name: '一等奖', angle: 0, probability: 5 }, // 5% 概率
        { name: '二等奖', angle: 36, probability: 10 }, // 10% 概率
        { name: '三等奖', angle: 72, probability: 15 }, // 15% 概率
        { name: '四等奖', angle: 108, probability: 10 }, // 10% 概率
        { name: '五等奖', angle: 144, probability: 5 }, // 5% 概率
        { name: '六等奖', angle: 180, probability: 20 }, // 20% 概率
        { name: '七等奖', angle: 216, probability: 10 }, // 10% 概率
        { name: '八等奖', angle: 252, probability: 10 }, // 10% 概率
        { name: '九等奖', angle: 288, probability: 5 }, // 5% 概率
        { name: '十等奖', angle: 324, probability: 10 } // 10% 概率
      ]
      // 计算总概率权重
      const totalProbability = prizes.reduce(
        (total, prize) => total + prize.probability,
        0
      )
      // 随机抽奖函数,基于权重
      function getRandomPrize() {
        const random = Math.random() * totalProbability
        let sum = 0
        for (let prize of prizes) {
          sum += prize.probability
          if (random <= sum) {
            return prize
          }
        }
      }
      // 转动指针的函数
      function spinPointer() {
        if (isSpinning) return // 如果正在转动,不允许重复点击

        isSpinning = true
        const selectedPrize = getRandomPrize()
        const spins = Math.floor(Math.random() * 5) + 5 // 随机转动5到10圈
        const totalRotation = spins * 360 + selectedPrize.angle // 指针旋转的总角度
        pointer.style.transition = 'transform 4s ease-out'
        pointer.style.transform = `translate(4%, -96%) rotate(${totalRotation}deg)`

        // 等待动画结束后提示中奖奖项
        setTimeout(() => {
          alert(`恭喜你获得了: ${selectedPrize.name}`)
          isSpinning = false // 允许再次点击
        }, 5000) // 动画时间为5秒
      }
    </script>
  </body>
</html>


http://www.kler.cn/news/342103.html

相关文章:

  • 基于SpringBoot“花开富贵”花园管理系统【附源码】
  • 深入理解Transformer的笔记记录(精简版本)---- Transformer
  • Vuex 使用实例
  • 多线程-进阶(1)
  • 胤娲科技:00后揭秘——AI大模型的可靠性迷局
  • Could not get JDBC Connection: wait millis 10000, active 500
  • 数字化AI新赋能,智享AI直播:开启一个全新的直播时代!
  • WPS的JS宏实现删除某级标题下的所有内容
  • 【大模型理论篇】精简循环序列模型(minGRU/minLSTM)性能堪比Transformer以及对循环神经网络的回顾
  • tp6的系统是如何上架的
  • Facebook直播分析与问题解决策略
  • 什么是「杀猪盘」?怎样能有效防范杀猪盘诈骗?
  • 带你深入浅出设计模式:十一、组合实体模式:软件世界的乐高积木
  • 日语学习零基础生活日语口语柯桥外语学校|股票用日语怎么说?
  • threejs-基础材质设置
  • 软件项目开发流程与团队分工整体认知——基于《信息系统项目管理师教程》(需求分析、系统设计、开发、测试、部署与运维、开发工具与管理软件)
  • 【C++篇】虚境探微:多态的流动诗篇,解锁动态的艺术密码
  • leetcode---素数,最小质因子,最大公约数
  • 回归分析在数据挖掘中的应用简析
  • x86 架构下一些常用的汇编指令英文全称与功能简述