uniapp实现简单的九宫格抽奖(附源码)

效果展示

uniapp实现大转盘抽奖

实现步骤:

1.该页面可设置8个奖品,每个奖品可设置中奖机会的权重,如下chance越大,中奖概率越高(大于0)

javascript 复制代码
// 示例代码
prizeList: [
	{
		id: 1,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品1",
		chance: 1
	},
	{
		id: 2,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品2",
		chance: 3
	},{
		id: 3,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品3",
		chance: 1
	},
	{
		id: 4,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品4",
		chance: 1
	},
	{
		id: 5,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品5",
		chance: 1
	},
	{
		id: 6,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品6",
		chance: 1
	},
	{
		id: 7,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品7",
		chance: 2
	},
	{
		id: 8,
		image: "https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp",
		name: "奖品8",
		chance: 1
	}
]

2.该页面8个奖品加上一个立即抽奖,一共9个格子,可使用flex-wrap布局排列,转到哪个格子就使用border高光标注

html 复制代码
<!-- 示例代码 -->
<view class="lottery">
	<block v-for="(item, index) in prizeList" :key="index">
		<image class="prize" :class="currentIndex==index?'active':''" src="https://img.alicdn.com/imgextra/i4/1939750137/O1CN01XZivln1CsmzUGGtQF_!!0-saturn_solar.jpg_468x468q75.jpg_.webp" />
		<view v-if="index==3" class="goLottery center_row" :style="btnDisabled?'opacity:0.5;':''" @tap="goLottery">
			<view>立即抽奖</view>
		</view>
	</block>
</view>

3.转动可多次使用不同时间的定时器setIntervalclearInterval结合,不断切换格子active的border位置

javascript 复制代码
// 示例代码
data() {
	return {
		currentIndex: 0,
		runIndexList: [0,1,2,4,7,6,5,3],
		lastRunStepList: [0,1,2,7,3,6,5,4],
		btnDisabled: false,
		prizeList: []
	}
},






let timer = setInterval(() => {
	count--
	currentIndex++
	this.currentIndex = (this.runIndexList[currentIndex % 8])
	if (count < this.prizeList.length * 2) {
		clearInterval(timer)
		let timer2 = setInterval(() => {
			count--
			currentIndex++
			this.currentIndex = (this.runIndexList[currentIndex % 8])
			if (count < this.prizeList.length * 1 - 3) {
				clearInterval(timer2)
				let timer3 = setInterval(() => {
					count--
					currentIndex++
					this.currentIndex = (this.runIndexList[currentIndex % 8])
					if (count <= 0) {
						clearInterval(timer3)
						this.btnDisabled = false
						uni.showToast({
							title: `恭喜您,抽中了${this.prizeList[this.currentIndex].name}`,
							icon: "none"
						})
					}
				}, 400)
			}
	}, 200)
	}
}, 100)

4.根据提前设定好的奖品概率change随机抽取奖品

javascript 复制代码
// 示例代码
// 返回抽奖结果奖品的index
getResultIndex() {
	let totalChance = 0
	for (let i in this.prizeList) {
		totalChance += Number(this.prizeList[i].chance ? this.prizeList[i].chance : 1)
	}
	// 0 ~ 1
	let random = Math.random()
	let index = 0
	let num = 0
	for (let i in this.prizeList) {
		num += (Number(this.prizeList[i].chance) / totalChance)
		if (random < num) {
			index = Number(i)
			break
		}
	}
	return index
}

大功告成!

有任何问题请留言咨询,着急请+企鹅1140559610有源码

相关推荐
说私域20 分钟前
无人零售及开源 AI 智能名片 S2B2C 商城小程序的深度剖析
人工智能·小程序·零售
Kika写代码13 小时前
【微信小程序】页面跳转基础 | 我的咖啡店-综合实训
服务器·微信小程序·小程序
源码哥_博纳软云15 小时前
JAVA同城服务场馆门店预约系统支持H5小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台
禾高网络15 小时前
租赁小程序成品|租赁系统搭建核心功能
java·人工智能·小程序
YUJIAN。16 小时前
使用uniapp开发微信小程序-框架搭建
微信小程序·小程序·uni-app
关你西红柿子18 小时前
小程序app封装公用顶部筛选区uv-drop-down
前端·javascript·vue.js·小程序·uv
V+zmm1013418 小时前
基于小程序宿舍报修系统的设计与实现ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·ssm
V+zmm101341 天前
基于微信小程序的乡村政务服务系统springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·ssm
还这么多错误?!1 天前
uniapp微信小程序,使用fastadmin完成一个一键获取微信手机号的功能
微信小程序·小程序·uni-app
_院长大人_1 天前
微信小程序用户信息解密 AES/CBC/NoPadding 解密失败问题
微信小程序·小程序