-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturntable.html
185 lines (168 loc) · 5.44 KB
/
turntable.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="http://code.jquery.com/jquery-1.12.4.js"></script>
<script src="./turntable.js"></script>
<title>抽奖</title>
<style>
.lottery {
position: relative;
display: inline-block;
}
.lottery img {
position: absolute;
top: 50%;
left: 50%;
margin-left: -76px;
margin-top: -82px;
cursor: pointer;
}
#message {
position: absolute;
top: 0px;
left: 10%;
}
</style>
<!--[if lte IE 8]>
<style>
.lottery img{
display: none;
}
</style>
<![endif]-->
</head>
<body>
<div class="lottery">
<canvas id="myCanvas" width="600" height="600" style="border:1px solid #d3d3d3;">
当前浏览器版本过低,请使用其他浏览器尝试
</canvas>
<p id="message"></p>
<img src="./images/start.png" id="start">
</div>
<script>
var wheelSurf
// 初始化装盘数据 正常情况下应该由后台返回
var initData = {
"success": true,
"list": [{
"id": 100,
"name": "5000元京东卡",
"image": "./images/1.png",
"rank":1,
"percent":3
},
{
"id": 101,
"name": "1000元京东卡",
"image": "./images/2.png",
"rank":2,
"percent":5
},
{
"id": 102,
"name": "100个比特币",
"image": "./images/3.png",
"rank":3,
"percent":2
},
{
"id": 103,
"name": "50元话费",
"image": "./images/4.png",
"rank":4,
"percent":49
},
{
"id": 104,
"name": "100元话费",
"image": "./images/5.png",
"rank":5,
"percent":30
},
{
"id": 105,
"name": "500个比特币",
"image": "./images/6.png",
"rank":6,
"percent":1
},
{
"id": 106,
"name": "500元京东卡",
"image": "./images/7.png",
"rank":7,
"percent":10
}
]
}
// 计算分配获奖概率(前提所有奖品概率相加100%)
function getGift(){
var percent = Math.random()*100
var totalPercent = 0
for(var i = 0 ,l = initData.list.length;i<l;i++){
totalPercent += initData.list[i].percent
if(percent<=totalPercent){
return initData.list[i]
}
}
}
var list = {}
var angel = 360 / initData.list.length
// 格式化成插件需要的奖品列表格式
for (var i = 0, l = initData.list.length; i < l; i++) {
list[initData.list[i].rank] = {
id:initData.list[i].id,
name: initData.list[i].name,
image: initData.list[i].image
}
}
// 查看奖品列表格式
// 定义转盘奖品
wheelSurf = $('#myCanvas').WheelSurf({
list: list, // 奖品 列表,(必填)
outerCircle: {
color: '#df1e15' // 外圈颜色(可选)
},
innerCircle: {
color: '#f4ad26' // 里圈颜色(可选)
},
dots: ['#fbf0a9', '#fbb936'], // 装饰点颜色(可选)
disk: ['#ffb933', '#ffe8b5', '#ffb933', '#ffd57c', '#ffb933', '#ffe8b5', '#ffd57c'], //中心奖盘的颜色,默认7彩(可选)
title: {
color: '#5c1e08',
font: '19px Arial'
} // 奖品标题样式(可选)
})
// 初始化转盘
wheelSurf.init()
// 抽奖
var throttle = true;
$("#start").on('click', function () {
var winData = getGift() // 正常情况下获奖信息应该由后台返回
$("#message").html('')
if(!throttle){
return false;
}
throttle = false;
var count = 0
// 计算奖品角度
for (const key in list) {
if (list.hasOwnProperty(key)) {
if (winData.id == list[key].id) {
break;
}
count++
}
}
// 转盘抽奖,
wheelSurf.lottery((count * angel + angel / 2), function () {
$("#message").html(winData.name)
throttle = true;
})
})
</script>
</body>
</html>