-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (80 loc) · 2.9 KB
/
script.js
File metadata and controls
88 lines (80 loc) · 2.9 KB
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
let slot_screen = document.getElementById("slot-screen");
let reel = document.getElementsByClassName("reel");
let reels = document.getElementsByClassName("reels");
let stop_btn = document.getElementsByClassName("stop-btn");
let start_btn = document.getElementById("start-btn");
let sec = 300; // slot reel rotation speed in milliseconds
let stopReelFlag = []; // slot reel stop flag
let reelCounts = []; // which image to position in the slot reel
let slotFrameHeight; // frame size of the slot reel
let slotReelsHeight; // overall reel(image) size
let slotReelItemHeight; // size of one reel(image)
let slotReelStartHeight; // initial image value
// slot reel stop flag initialization
let slot = {
init:function() {
stopReelFlag[0] = stopReelFlag[1] = stopReelFlag[2] = false;
reelCounts[0] = reelCounts[1] = reelCounts[2] = 0;
},
// click event of start button
start:function() {
slot.init();
for(let index = 0; index < 3; index++) {
slot.animation(index);
}
},
// stop button click event
stop:function(i) {
stopReelFlag[i] = true;
if(stopReelFlag[0] && stopReelFlag[1] && stopReelFlag[2]) {
start_btn.removeAttribute("disabled");
}
},
// set first position of slot reel
resetlocationInfo:function() {
slotFrameHeight = slot_screen.offsetHeight;
slotReelsHeight = reels[0].offsetHeight;
slotReelItemHeight = reel[0].offsetHeight;
slotReelStartHeight = --slotReelsHeight;
slotReelStartHeight = (slotFrameHeight / 2) - (slotReelItemHeight * 1.5);
for(let i = 0; i < reels.length; i++) {
reels[i].style.top = String(slotReelStartHeight) + "px";
}
},
// move slot reel
animation: function(index) {
if (reelCounts[index] >= 8) {
reelCounts[index] = 0;
$(".reels").eq(index).css("top", String(slotReelStartHeight - (10 * slotReelItemHeight)) + "px");
}
$(".reels").eq(index).animate({
"top": slotReelStartHeight - ((reelCounts[index] + 1) * slotReelItemHeight) + "px"
}, {
duration: sec,
easing: "linear",
complete: function() {
if (stopReelFlag[index]) {
return;
}
reelCounts[index]++;
slot.animation(index);
}
});
}
};
window.onload = function() {
slot.init();
slot.resetlocationInfo();
start_btn.addEventListener("click", function(e) {
e.target.setAttribute("disabled", true);
slot.start();
for (let i = 0; i < stop_btn.length; i++) {
stop_btn[i].removeAttribute("disabled");
}
});
for (let i = 0; i < stop_btn.length; i++) {
stop_btn[i].addEventListener("click", function(e) {
slot.stop(e.target.getAttribute("data-val"));
});
}
};