vue实现简单转盘抽奖功能
作者:jiyuchengzhou
这篇文章主要为大家详细介绍了vue实现简单转盘抽奖功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了vue实现简单转盘抽奖的具体代码,供大家参考,具体内容如下

样式请大家忽略(自己调),主要看JS代码实现,点击按钮后调用start方法,判断是否在转动状态,如果没转动则调用go方法,go方法主要封装了一次性定时器,是个递归函数,调用go函数后即可实现抽奖转盘的效果了,详细代码如下:
注释清晰哦
<template>
<div class="home">
<button @click="start">开始</button>
<div
class="grid"
v-for="(item, i) in arr"
:key="i"
:class="[bg == i + 1 ? 'active' : null]"
></div>
</div>
</template>
<script>
export default {
name: "Home",
data() {
return {
// 标记转动的位置
bg: 1,
// 循环的奖品数组
arr: [1, 2, 3, 4, 5],
// 是否正在转动的标记
isSport: false,
// 转动速度减慢
reduce: 10,
// 转动间隔时间
startTime: 30,
area:''
};
},
methods: {
start() {
if (this.isSport == false) {
this.isSport = true;
} else {
// 正在转动时点击按钮无效
return;
}
// 模拟的设定转动的位置
this.area= parseInt(Math.random()*4+1);
this.go();
},
go() {
setTimeout(() => {
// 让转动速度减慢
this.startTime = this.startTime + this.reduce;
this.bg = (this.bg % 5) + 1;//这个%时求余的意识哦
// 如果达到这个条件则停止跳动
if (this.startTime >= 300 && this.bg == this.area) {
// 标记是否转动状态
this.isSport = false;
// 重置转动间隔时间
this.startTime = 30;
return;
}
this.go();
}, this.startTime);
},
},
};
</script>
<style scoped>
.grid {
width: 50px;
height: 50px;
background: red;
margin: 10px;
}
.active {
background: blue;
}
</style>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
