【黄啊码】小程序:九宫格抽奖如何实现?可控制抽奖率

【黄啊码】小程序:九宫格抽奖如何实现?可控制抽奖率

黄啊码向来简单粗暴,来,代码伺候

js代码如下:

php;gutter:true; //index.js //获取应用实例 const app = getApp()</p> <p>//计数器 var interval = null;</p> <p>//值越大旋转时间越长 即旋转速度 var intime = 50;</p> <p>Page({ data: { color: [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], //9张奖品图片 images: [], btnconfirm: '/images/dianjichoujiang.png', clickLuck:'clickLuck', luckPosition:2, prizeWeight:[], titles:[], },</p> <p>onLoad:function(){ var that=this; wx.request({ url: 'XXXXXXX', method:'POST', header: { 'content-type': 'application/json' // 默认值 }, data:{ },success(res){ that.setData({prizeWeight:res.data.data.percent_list});//奖项权重数组,表征各奖项的中奖机会占总数的百分比。比如一等奖的中奖率是1%,二等奖的中奖率是5% that.setData({images:res.data.data.images_list}); that.setData({titles:res.data.data.title_list}); //例子:prizeWeight:({5,5,10,20,30,10,10,10})记住是按顺序的,所有的加起来等于100,images就是八张图片的地址,titles就是八个标题的内容</p> <p>} })</p> <p>},</p> <p>//点击抽奖按钮 clickLuck:function(){ var e = this; var weightSum = e.data.prizeWeight.reduce(function(prev, currVal){ //计算权重之和:1+5+20+74=100 return prev + currVal; //prev 是前一次累加后的数值,currVal 是本次待加的数值 }, 0); console.log(weightSum); var random = Math.random()*weightSum; console.log(random); var concatWeightArr = e.data.prizeWeight.concat(random); //将随机数加入权重数组 console.log(concatWeightArr); var sortedWeightArr = concatWeightArr.sort(function(a, b){return a-b;}); //将包含随机数的新权重数组按从小到大(升序)排序 console.log(sortedWeightArr); var randomIndex = sortedWeightArr.indexOf(random); //索引随机数在新权重数组中的位置 console.log(randomIndex); randomIndex = Math.min(randomIndex, e.data.images.length -1); //权重随机数的下标不得超过奖项数组的长度-1,重新计算随机数在奖项数组中的索引位置 console.log(randomIndex); e.setData({luckPosition:randomIndex}); //设置按钮不可点击 e.setData({ btnconfirm:'/images/dianjichoujiangd.png', clickLuck:'', }) //清空计时器 clearInterval(interval); var index = 0; console.log(e.data.color[0]); //循环设置每一项的透明度 interval = setInterval(function () { if (index > 7) { index = 0; e.data.color[7] = 0.5 } else if (index != 0) { e.data.color[index - 1] = 0.5 } e.data.color[index] = 1 e.setData({ color: e.data.color, }) index++; }, intime);</p> <p>//模拟网络请求时间 设为两秒 var stoptime = 2000; setTimeout(function () { e.stop(e.data.luckPosition); }, stoptime)</p> <p>},</p> <p>stop: function (which){ var e = this; //清空计数器 clearInterval(interval); //初始化当前位置 var current = -1; var color = e.data.color; for (var i = 0; i < color.length; i++) { if (color[i] == 1) { current = i; } } //下标从1开始 var index = current + 1;</p> <p>e.stopLuck(which, index, intime, 10); },</p> <p>/*<em> * which:中奖位置 * index:当前位置 * time:时间标记 * splittime:每次增加的时间 值越大减速越快 </em>/ stopLuck: function (which, index,time,splittime){ var e = this; //值越大出现中奖结果后减速时间越长 var color = e.data.color; setTimeout(function () { //重置前一个位置 if (index > 7) { index = 0; color[7] = 0.5 } else if (index != 0) { color[index - 1] = 0.5 } //当前位置为选中状态 color[index] = 1 e.setData({ color: color, }) //如果旋转时间过短或者当前位置不等于中奖位置则递归执行 //直到旋转至中奖位置 if (time < 400 || index != which){ //越来越慢 splittime++; time += splittime; //当前位置+1 index++; e.stopLuck(which, index, time, splittime); }else{ //1秒后显示弹窗 setTimeout(function () { wx.showModal({ title: '提示', content: e.data.titles[which], showCancel: false, success: function (res) { if (res.confirm) { //设置按钮可以点击 e.setData({ btnconfirm: '/images/dianjichoujiang.png', clickLuck: 'clickLuck', }) } } })</p> <p>}, 1000); } }, time); console.log(time); } })</p> <pre><code> 前端: </code></pre> <p>class="container"> class='frame_view'> class='frame_row'> class='frame_item' style='opacity:{{color[0]}}' src='{{images[0]}}'> class='frame_item' style='opacity:{{color[1]}}' src='{{images[1]}}'> class='frame_item' style='opacity:{{color[2]}}' src='{{images[2]}}'></p> <p>class='frame_row'> class='frame_item' style='opacity:{{color[7]}}' src='{{images[7]}}'> class='frame_item' src='{{btnconfirm}}' bindtap='{{clickLuck}}'> class='frame_item' style='opacity:{{color[3]}}' src='{{images[3]}}'></p> <p>class='frame_row'> class='frame_item' style='opacity:{{color[6]}}' src='{{images[6]}}'> class='frame_item' style='opacity:{{color[5]}}' src='{{images[5]}}'> class='frame_item' style='opacity:{{color[4]}}' src='{{images[4]}}'></p> <pre><code> wxss: </code></pre> <p>/<strong>index.wxss</strong>/ .frame_view{ bottom: 160rpx; left: 60rpx; right: 60rpx; width:590rpx; height:590rpx; padding: 20rpx; background: #792db3; z-index: 3; display: flex; flex-direction: column; justify-content: space-between; align-items: center; border-radius: 30rpx; } .frame_row{ width:580rpx; height:180rpx; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .frame_item{ width:180rpx; height:180rpx; }

嗯,就这样,如果还不会,扫我码,戳我头像,使劲戳,OK?

Original: https://www.cnblogs.com/huangama/p/15650951.html
Author: 黄啊码
Title: 【黄啊码】小程序:九宫格抽奖如何实现?可控制抽奖率

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/619713/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • django中的JsonRseponse对象

    json格式的数据 在进行前后端数据交互的时候,我们需要使用json格式的数据作为过渡,实现跨语言传输数据! django中的JsonResponse对象 在django中Json…

    数据库 2023年6月14日
    085
  • Linux(CentOS)安装Redis保姆级教程

    Linux(CentOs)安装Redis教程 一,下载Redis(两种方式) 1,找到redis官网(https://redis.io/download ) 如果想下载指定版本就去…

    数据库 2023年6月11日
    085
  • Redis——数据操作(3)

    2022-09-22 (4)set操作 ①添加操作(sadd): 例: ②查看操作(smembers):例: ③移除操作(srem):例: (5)zset(有序集合而且里面的元素是…

    数据库 2023年6月14日
    0131
  • MySQL学习笔记(七)–Index Merge

    什么是Index Merge The Index Merge access method retrieves rows with multiple range scans and …

    数据库 2023年6月16日
    081
  • Celery异步任务

    (1)安装celery pip install celery==4.2.1 (2)celery使用 在项目适当位置创建celery_tasks目录用于保存celery异步任务。 在…

    数据库 2023年6月14日
    076
  • SQL的约束

    概念:约束是作用于表中的字段以限制表中数据存储的规则 [En] concept: constraints are rules that act on fields in a tab…

    数据库 2023年5月24日
    099
  • 设计模式之(4)——单例模式

    定义:单例模式属于创建型模式,该类负责创建自己的对象实例,并且确保只有单个对象被创建,同时该类提供了一种全局访问其唯一实例对象的方式;这个定义中有三个要点:1、单例类只能有一个实例…

    数据库 2023年6月14日
    084
  • WIN10下启动VMware虚拟机蓝屏的解决办法

    问题: 每次启动虚拟机就会蓝屏,提示错误代码: PAGE_FAULT_IN_NONPAGED_AREA 解决办法: 禁用 Hyper-V 功能 打开”控制面板&#821…

    数据库 2023年6月14日
    087
  • etcd和Zookeeper孰优孰劣对比

    背景 最近在看到Pachyderm的介绍时,看到作者拿YARN和Kubernetes做类比,拿Zookeeper和etcd做对比。YARN和Kubernetes的类比还相对比较好理…

    数据库 2023年6月11日
    096
  • ShardingSphere 异构迁移最佳实践:将3.5亿量级的顾客系统 RTO 减少60倍

    Apache ShardingSphere 助力当当 3.5 亿用户量级顾客系统重构,由 PHP+SQL Server 技术栈无缝转型为 Java+ShardingSphere+M…

    数据库 2023年6月16日
    0114
  • AspNetCoreapi 使用 Docker + Centos 7部署

    好久没有更新文章了,前段时间写了一系列的文章放到桌面了,想着修修改改,后来系统中勒索病毒了还被公司网络安全的抓到是我电脑,后来装系统文章给装丢了。然后好长一段时间没有写了。 今天记…

    数据库 2023年6月11日
    095
  • 实用技术博客收集

    作者:sczyh30java全栈知识体系microsoft cloud design pattern Original: https://www.cnblogs.com/rache…

    数据库 2023年6月11日
    0115
  • Linux中的进程的服务和控制

    Linux中的进程 进程:已经启动的可执行程序的运行实例 1、PID:进程的ID(每一个新进程都有一个唯一的PID) 2、PPID:父进程的ID 3、任何一个进程都可以创建一个子进…

    数据库 2023年6月16日
    052
  • Cobar提出的一种在分库场景下对Order By / Limit 的优化

    搜索关注微信公众号”捉虫大师”,后端技术分享,架构设计、性能优化、源码阅读、问题排查、踩坑实践。本文已收录 https://github.com/lkxia…

    数据库 2023年5月24日
    0113
  • Node.js安装

    nvm NVM: Node Version Manager 下载地址 Mac/Linux安装 nvm:https://github.com/nvm-sh/nvm Windows 安…

    数据库 2023年6月6日
    090
  • 我的第一本算法书 第一章

    1.1 决定数据顺序和位置关系的是数据结构 电话簿的数据结构 按获取顺序排序 按拼音顺序排序 添加简单 查询麻烦 查询简单 添加麻烦 两者结合 分别使用不同的表存储不同的首字母, …

    数据库 2023年6月11日
    093
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球