本文实例为大家分享了jquery实现抽奖功能的具体代码,供大家参考,具体内容如下
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 > < head > < meta charset = "utf-8" > < title ></ title > < style type = "text/css" > #lottery { width: 570px; height: 510px; margin: 0px auto; border: 4px solid #ba1809; } #lottery table { background-color: yellow; } #lottery table td { position: relative; width: 190px; height: 170px; text-align: center; color: #333; font-index: -999 } #lottery table td img { display: block; width: 190px; height: 170px; } #lottery table td a { width: 190px; height: 170px; display: block; text-decoration: none; background: url(img/9.jpg) no-repeat top center; } #lottery table td a:hover { background-image: url(img/11.jpg); } #lottery table td.active .mask { display: block; } .mask { width: 100%; height: 100%; position: absolute; left: 0; top: 0; background-color: rgba(252, 211, 4, 0.5); display: none; } </ style > </ head > < body > < div id = "lottery" > < table border = "0" cellpadding = "0" cellspacing = "0" > < tr > < td class = "lottery-unit lottery-unit-0" > < img src = "img/1.jpg" > < div class = "mask" ></ div > </ td > < td class = "lottery-unit lottery-unit-1" > < img src = "img/2.jpg" > < div class = "mask" ></ div > </ td > < td class = "lottery-unit lottery-unit-2" > < img src = "img/3.jpg" > < div class = "mask" ></ div > </ td > </ tr > < tr > < td class = "lottery-unit lottery-unit-7" > < img src = "img/4.jpg" > < div class = "mask" ></ div > </ td > <!-- 点击触发抽奖 --> < td >< a href = "#" rel = "external nofollow" ></ a ></ td > < td class = "lottery-unit lottery-unit-3" > < img src = "img/5.jpg" > < div class = "mask" ></ div > </ td > </ tr > < tr > < td class = "lottery-unit lottery-unit-6" > < img src = "img/6.jpg" > < div class = "mask" ></ div > </ td > < td class = "lottery-unit lottery-unit-5" > < img src = "img/7.jpg" > < div class = "mask" ></ div > </ td > < td class = "lottery-unit lottery-unit-4" > < img src = "img/8.jpg" > < div class = "mask" ></ div > </ td > </ tr > </ table > </ div > < script src = "js/jquery.js" ></ script > < script type = "text/javascript" > var lot = $(".lottery-unit"); var nowIndex = -1; //记录添加激活类的下标 var btn = $("table").find("a") console.log(btn) var curIndex = null; //记录上一次坐标 var round = 0; //记录移动几圈 var n = 0; //记录移动了多少次 var timer = null; //旋转计时器 var priceIndex = (Math.random()*lot.length) | 0; //中奖的下标 console.log(priceIndex) var isClick = true; function move(){ n++; nowIndex++; if(n%8==0){ round++; console.log("跑了"+round+"圈"); if(round>=3){ clearInterval(timer); timer = setInterval(move,50) } if(round > 8){ clearInterval(timer); timer = setInterval(move,1000) } } // 第二种方式 // if(n>=8 && n< 12 ){ // clearInterval(timer) // timer = setInterval (move,50) // } // if(n>=12){ // clearInterval(timer) // timer = setInterval(move,50) // } lot.filter(".lottery-unit-"+nowIndex).addClass("active") // 当curIndex为0时,布尔值为false,所以要加curIndex==0 if(curIndex || curIndex==0){ lot.filter(".lottery-unit-"+curIndex).removeClass("active") } curIndex = nowIndex; // 如何实现中奖 if(nowIndex == priceIndex && round > 8){ clearInterval(timer); if(priceIndex==1){ setTimeout(function(){ alert("111111") },1000) } // 重置参数 isClick = true; round = 0; nowIndex = -1; curIndex = null; priceIndex = (Math.random()*lot.length) | 0; console.log("中奖的下标",priceIndex) } if(nowIndex>=lot.length-1){ nowIndex=-1; } } btn.click(function(){ if(isClick){ console.log("开始抽奖"); isClick = false; timer = setInterval(move,100); } }) </ script > </ body > </ html > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/maxiaoxin1314/article/details/104506657