-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (63 loc) · 2.43 KB
/
script.js
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
document.getElementById("drawButton").addEventListener("click", () => {
const gifts = document.querySelectorAll(".gift");
let currentIndex = 0;
let speed = 400; // 初始速度(毫秒)
let interval;
// 開始輪流高亮顯示
function highlightNext() {
gifts.forEach((gift, index) => {
gift.classList.toggle("highlight-border", index === currentIndex); // 僅應用邊框高亮
});
currentIndex = (currentIndex + 1) % gifts.length;
}
// 動態調整速度的函數
function dynamicSpeedAdjust(duration) {
const maxSpeed = 100; // 最快速度(毫秒)
const minSpeed = 400; // 最慢速度(毫秒)
const midTime = duration / 2; // 加速和減速的分界點
const elapsed = Date.now() - startTime;
if (elapsed < midTime) {
// 前半段加速
speed = Math.max(maxSpeed, minSpeed - (elapsed / midTime) * (minSpeed - maxSpeed));
} else {
// 後半段減速
speed = Math.min(minSpeed, maxSpeed + ((elapsed - midTime) / midTime) * (minSpeed - maxSpeed));
}
}
// 啟動輪詢,並動態調整速度
const startTime = Date.now();
const duration = 5000; // 總輪詢時間(毫秒)
function startRoulette() {
highlightNext();
dynamicSpeedAdjust(duration);
if (Date.now() - startTime < duration) {
interval = setTimeout(startRoulette, speed);
} else {
// 停止輪詢並選擇最終獎品
const randomIndex = Math.floor(Math.random() * gifts.length);
gifts.forEach((gift, index) => {
if (index === randomIndex) {
gift.classList.add("highlight-border"); // 僅保留邊框高亮
setTimeout(() => {
// 顯示中獎訊息並移動到正中間
gifts.forEach((gift, idx) => {
if (idx !== randomIndex) gift.classList.add("fade-out");
});
setTimeout(() => {
const selectedGift = gifts[randomIndex].innerHTML;
document.getElementById("page2").innerHTML = `
<h1>恭喜小寶貝!你抽到:</h1>
<div class="selected">
${selectedGift}
</div>
`;
}, 1000);
}, 1000);
} else {
gift.classList.remove("highlight-border");
}
});
}
}
startRoulette();
});