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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="zh-Hant">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>幸運抽獎機</title>
|
| 7 |
+
<!-- Load Tailwind CSS -->
|
| 8 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 9 |
+
<style>
|
| 10 |
+
/* Custom styles for the slot machine effect */
|
| 11 |
+
.slot-container {
|
| 12 |
+
position: relative;
|
| 13 |
+
height: 300px; /* Fixed viewport height */
|
| 14 |
+
overflow: hidden;
|
| 15 |
+
background: linear-gradient(135deg, #1f2937, #374151); /* Dark background */
|
| 16 |
+
border: 4px solid #fcd34d; /* Yellow border */
|
| 17 |
+
box-shadow: 0 0 20px rgba(252, 211, 77, 0.5);
|
| 18 |
+
display: flex;
|
| 19 |
+
justify-content: center;
|
| 20 |
+
align-items: center;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
.slot-track {
|
| 24 |
+
width: 100%;
|
| 25 |
+
transform: translateY(0);
|
| 26 |
+
/* 預設無過渡效果,快速移動由JS控制 */
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.slot-item {
|
| 30 |
+
height: 60px; /* Height of each participant entry */
|
| 31 |
+
display: flex;
|
| 32 |
+
align-items: center;
|
| 33 |
+
justify-content: center;
|
| 34 |
+
color: white;
|
| 35 |
+
font-size: 1.875rem; /* text-3xl, 轉動中字體加大 */
|
| 36 |
+
font-weight: 700; /* font-bold */
|
| 37 |
+
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
| 38 |
+
user-select: none;
|
| 39 |
+
will-change: transform; /* 提升動畫效能 */
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/* Highlight for the winning item */
|
| 43 |
+
.winner-highlight {
|
| 44 |
+
background-color: #fcd34d;
|
| 45 |
+
color: #1f2937;
|
| 46 |
+
transition: background-color 0.3s;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
.slot-indicator-line {
|
| 50 |
+
position: absolute;
|
| 51 |
+
top: 50%;
|
| 52 |
+
transform: translateY(-50%);
|
| 53 |
+
width: 100%;
|
| 54 |
+
height: 60px; /* Same height as slot item */
|
| 55 |
+
border-top: 2px solid #fcd34d;
|
| 56 |
+
border-bottom: 2px solid #fcd34d;
|
| 57 |
+
box-shadow: 0 0 15px rgba(252, 211, 77, 0.8);
|
| 58 |
+
pointer-events: none;
|
| 59 |
+
z-index: 10;
|
| 60 |
+
background-color: rgba(255, 255, 255, 0.05); /* Subtle highlight */
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
.slot-mask {
|
| 64 |
+
position: absolute;
|
| 65 |
+
top: 0;
|
| 66 |
+
left: 0;
|
| 67 |
+
width: 100%;
|
| 68 |
+
height: 100%;
|
| 69 |
+
/* Gradient mask to fade out items above/below the center */
|
| 70 |
+
mask-image: linear-gradient(to bottom, transparent, black 20%, black 80%, transparent);
|
| 71 |
+
background-color: rgba(0, 0, 0, 0.2); /* Dark overlay */
|
| 72 |
+
pointer-events: none;
|
| 73 |
+
border-radius: 8px;
|
| 74 |
+
z-index: 9;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
/* Override Tailwind for custom scrollbar aesthetics */
|
| 78 |
+
textarea::-webkit-scrollbar {
|
| 79 |
+
width: 8px;
|
| 80 |
+
}
|
| 81 |
+
textarea::-webkit-scrollbar-thumb {
|
| 82 |
+
background-color: #fcd34d;
|
| 83 |
+
border-radius: 4px;
|
| 84 |
+
}
|
| 85 |
+
</style>
|
| 86 |
+
</head>
|
| 87 |
+
<body class="bg-gray-100 min-h-screen p-4 sm:p-8 font-sans">
|
| 88 |
+
|
| 89 |
+
<div id="app" class="max-w-6xl mx-auto">
|
| 90 |
+
<h1 class="text-4xl font-extrabold text-gray-800 mb-6 text-center">🎁 幸運抽獎機 🎁</h1>
|
| 91 |
+
|
| 92 |
+
<div class="flex flex-col lg:flex-row gap-8">
|
| 93 |
+
<!-- Left Side: Controls and Participants Input -->
|
| 94 |
+
<div class="lg:w-1/3 p-6 bg-white shadow-xl rounded-xl border border-gray-200">
|
| 95 |
+
<h2 class="text-2xl font-semibold mb-4 text-gray-700">1. 參加者名單輸入</h2>
|
| 96 |
+
<p class="text-sm text-gray-500 mb-3">格式:座號 姓名 (每行一組)</p>
|
| 97 |
+
<textarea id="participantsInput" rows="10" class="w-full p-3 border border-gray-300 rounded-lg focus:ring-amber-500 focus:border-amber-500 text-sm font-mono" placeholder="範例:
|
| 98 |
+
1 王小明
|
| 99 |
+
2 李大華
|
| 100 |
+
3 陳美玲
|
| 101 |
+
..."></textarea>
|
| 102 |
+
|
| 103 |
+
<button onclick="loadParticipants()" class="mt-4 w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-4 rounded-lg shadow-md transition duration-200">
|
| 104 |
+
載入名單 (共 <span id="participantCount">0</span> 人)
|
| 105 |
+
</button>
|
| 106 |
+
|
| 107 |
+
<h2 class="text-2xl font-semibold mt-8 mb-4 text-gray-700">2. 抽獎控制</h2>
|
| 108 |
+
<div id="statusMessage" class="p-3 bg-red-100 text-red-700 rounded-lg text-sm mb-4 hidden"></div>
|
| 109 |
+
|
| 110 |
+
<button id="startButton" onclick="startDraw()" class="w-full bg-amber-500 hover:bg-amber-600 text-gray-900 font-bold py-3 px-4 rounded-lg shadow-lg transition duration-200 disabled:opacity-50" disabled>
|
| 111 |
+
開始抽獎!
|
| 112 |
+
</button>
|
| 113 |
+
</div>
|
| 114 |
+
|
| 115 |
+
<!-- Right Side: Slot Machine Display -->
|
| 116 |
+
<div class="lg:w-2/3 p-6 bg-white shadow-xl rounded-xl">
|
| 117 |
+
<h2 class="text-2xl font-semibold mb-4 text-gray-700 text-center">幸運拉霸模擬</h2>
|
| 118 |
+
<div id="slotMachineContainer" class="slot-container rounded-lg">
|
| 119 |
+
<div id="slotTrack" class="slot-track">
|
| 120 |
+
<!-- 抽獎名單將動態載入並重複以模擬拉霸效果 -->
|
| 121 |
+
</div>
|
| 122 |
+
<!-- Center indicator line -->
|
| 123 |
+
<div class="slot-indicator-line"></div>
|
| 124 |
+
<div class="slot-mask"></div>
|
| 125 |
+
</div>
|
| 126 |
+
|
| 127 |
+
<div id="winnerOutput" class="mt-6 p-4 text-center bg-gray-800 text-white rounded-xl shadow-2xl scale-0 transition-transform duration-500 ease-out">
|
| 128 |
+
<p class="text-xl font-bold">🎉 幸運得主 🎉</p>
|
| 129 |
+
<p id="winnerResult" class="text-4xl font-extrabold text-amber-400 mt-2">N/A</p>
|
| 130 |
+
</div>
|
| 131 |
+
</div>
|
| 132 |
+
</div>
|
| 133 |
+
</div>
|
| 134 |
+
|
| 135 |
+
<script>
|
| 136 |
+
const participantsInput = document.getElementById('participantsInput');
|
| 137 |
+
const participantCountSpan = document.getElementById('participantCount');
|
| 138 |
+
const startButton = document.getElementById('startButton');
|
| 139 |
+
const statusMessage = document.getElementById('statusMessage');
|
| 140 |
+
const winnerOutput = document.getElementById('winnerOutput');
|
| 141 |
+
const winnerResult = document.getElementById('winnerResult');
|
| 142 |
+
|
| 143 |
+
// Slot Machine elements
|
| 144 |
+
const slotMachineContainer = document.getElementById('slotMachineContainer');
|
| 145 |
+
const slotTrack = document.getElementById('slotTrack');
|
| 146 |
+
|
| 147 |
+
let participants = [];
|
| 148 |
+
let slotItems = []; // Array of all items in the slot track (many repetitions)
|
| 149 |
+
let cheatingNumber = null;
|
| 150 |
+
let isDrawing = false;
|
| 151 |
+
let currentY = 0; // Tracking vertical position for JS animation
|
| 152 |
+
let animationFrameId = null; // For the requestAnimationFrame loop
|
| 153 |
+
|
| 154 |
+
const itemHeight = 60; // Must match CSS .slot-item height
|
| 155 |
+
const spinCycles = 30; // Number of times to repeat the list for smooth spinning
|
| 156 |
+
const fastSpinDuration = 4000; // 快速旋轉階段的時間 (4.0秒)
|
| 157 |
+
const slowdownDuration = 4000; // 減速到停止的階段時間 (4.0秒)
|
| 158 |
+
|
| 159 |
+
// --- Core Setup & State Management ---
|
| 160 |
+
|
| 161 |
+
/**
|
| 162 |
+
* Parses the textarea content, prepares slot items, and updates the UI.
|
| 163 |
+
*/
|
| 164 |
+
function loadParticipants() {
|
| 165 |
+
participants = [];
|
| 166 |
+
slotTrack.innerHTML = '';
|
| 167 |
+
slotItems = [];
|
| 168 |
+
|
| 169 |
+
// Reset UI
|
| 170 |
+
winnerOutput.classList.remove('scale-100');
|
| 171 |
+
winnerOutput.classList.add('scale-0');
|
| 172 |
+
winnerResult.textContent = 'N/A';
|
| 173 |
+
cheatingNumber = null;
|
| 174 |
+
|
| 175 |
+
// 1. Parsing logic
|
| 176 |
+
const lines = participantsInput.value.trim().split('\n').filter(line => line.trim() !== '');
|
| 177 |
+
|
| 178 |
+
lines.forEach(line => {
|
| 179 |
+
const parts = line.trim().split(/\s+/);
|
| 180 |
+
if (parts.length >= 2) {
|
| 181 |
+
const id = parseInt(parts[0], 10);
|
| 182 |
+
const name = parts.slice(1).join(' ');
|
| 183 |
+
|
| 184 |
+
if (!isNaN(id)) {
|
| 185 |
+
participants.push({ id, name, display: `${id} - ${name}` });
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
});
|
| 189 |
+
|
| 190 |
+
// 2. Create the repeating slot track items
|
| 191 |
+
if (participants.length > 0) {
|
| 192 |
+
// Repeat the list 'spinCycles' times
|
| 193 |
+
for (let cycle = 0; cycle < spinCycles; cycle++) {
|
| 194 |
+
participants.forEach(p => {
|
| 195 |
+
const item = createSlotItem(p.id, p.display);
|
| 196 |
+
slotTrack.appendChild(item);
|
| 197 |
+
slotItems.push({ id: p.id, element: item });
|
| 198 |
+
});
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
// Add the final section (the last repetition is where we stop)
|
| 202 |
+
participants.forEach(p => {
|
| 203 |
+
const item = createSlotItem(p.id, p.display);
|
| 204 |
+
slotTrack.appendChild(item);
|
| 205 |
+
slotItems.push({ id: p.id, element: item });
|
| 206 |
+
});
|
| 207 |
+
|
| 208 |
+
participantCountSpan.textContent = participants.length;
|
| 209 |
+
startButton.disabled = false;
|
| 210 |
+
statusMessage.classList.add('hidden');
|
| 211 |
+
|
| 212 |
+
} else {
|
| 213 |
+
startButton.disabled = true;
|
| 214 |
+
showStatus('請輸入有效的參加者名單。', 'bg-red-100 text-red-700');
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
/**
|
| 219 |
+
* Creates the HTML element for a single slot item.
|
| 220 |
+
*/
|
| 221 |
+
function createSlotItem(id, displayText) {
|
| 222 |
+
const item = document.createElement('div');
|
| 223 |
+
item.className = `slot-item`;
|
| 224 |
+
item.textContent = displayText;
|
| 225 |
+
item.setAttribute('data-id', id);
|
| 226 |
+
return item;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
/**
|
| 230 |
+
* Handles the secret keyboard input for cheating (invisible to users).
|
| 231 |
+
* @param {KeyboardEvent} event
|
| 232 |
+
*/
|
| 233 |
+
function handleKeydown(event) {
|
| 234 |
+
if (isDrawing) return;
|
| 235 |
+
|
| 236 |
+
// Check if the key is a digit (0-9)
|
| 237 |
+
if (event.key >= '0' && event.key <= '9') {
|
| 238 |
+
if (!cheatingNumber) cheatingNumber = '';
|
| 239 |
+
|
| 240 |
+
// Max length for input buffer
|
| 241 |
+
if (cheatingNumber.length < 5) {
|
| 242 |
+
cheatingNumber += event.key;
|
| 243 |
+
console.log('Secret input buffer:', cheatingNumber);
|
| 244 |
+
// No visual feedback is given to maintain secrecy
|
| 245 |
+
}
|
| 246 |
+
} else if (event.key === 'Backspace' || event.key === 'Delete') {
|
| 247 |
+
// Allow backspace to clear the last digit
|
| 248 |
+
if (cheatingNumber && cheatingNumber.length > 0) {
|
| 249 |
+
cheatingNumber = cheatingNumber.slice(0, -1);
|
| 250 |
+
console.log('Secret input buffer cleared one digit.');
|
| 251 |
+
}
|
| 252 |
+
} else if (event.key === 'Escape') {
|
| 253 |
+
// Clear the cheating number on Escape
|
| 254 |
+
cheatingNumber = null;
|
| 255 |
+
console.log('Secret input cleared.');
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
// --- Slot Control Functions ---
|
| 260 |
+
|
| 261 |
+
/**
|
| 262 |
+
* Animates the slot machine spin using requestAnimationFrame for smoothness.
|
| 263 |
+
* @param {Object} winnerData - Data about the winner and timing.
|
| 264 |
+
* @param {number} speed - Vertical speed in pixels per frame (negative value means moving up).
|
| 265 |
+
*/
|
| 266 |
+
function animateSpin(winnerData, speed) {
|
| 267 |
+
if (!isDrawing) return;
|
| 268 |
+
|
| 269 |
+
const oneBlockHeight = participants.length * itemHeight;
|
| 270 |
+
const elapsed = performance.now() - winnerData.startTime;
|
| 271 |
+
|
| 272 |
+
// Phase 1: Fast Spin
|
| 273 |
+
if (elapsed < fastSpinDuration) {
|
| 274 |
+
currentY -= speed;
|
| 275 |
+
|
| 276 |
+
// Wrap around
|
| 277 |
+
if (currentY < -oneBlockHeight) {
|
| 278 |
+
currentY += oneBlockHeight;
|
| 279 |
+
}
|
| 280 |
+
slotTrack.style.transform = `translateY(${currentY}px)`;
|
| 281 |
+
animationFrameId = requestAnimationFrame(() => animateSpin(winnerData, speed));
|
| 282 |
+
} else {
|
| 283 |
+
// Phase 2: Deceleration Trigger (Stop JS and use CSS transition)
|
| 284 |
+
if (animationFrameId) cancelAnimationFrame(animationFrameId);
|
| 285 |
+
|
| 286 |
+
// 1. Calculate the final target Y to center the winner
|
| 287 |
+
const centerOffset = (slotMachineContainer.clientHeight / 2) - (itemHeight / 2);
|
| 288 |
+
const totalItemsBeforeFinalBlock = participants.length * spinCycles;
|
| 289 |
+
const targetItemIndex = totalItemsBeforeFinalBlock + winnerData.index;
|
| 290 |
+
const finalTargetY = -(targetItemIndex * itemHeight) + centerOffset;
|
| 291 |
+
|
| 292 |
+
// 2. Determine the current position's offset within one block
|
| 293 |
+
let currentOffset = currentY % oneBlockHeight;
|
| 294 |
+
if (currentOffset > 0) currentOffset -= oneBlockHeight; // Ensure negative offset
|
| 295 |
+
|
| 296 |
+
// 3. Calculate a "snap point": The Y position corresponding to the start of the final block, plus the current offset.
|
| 297 |
+
const snapPoint = -(totalItemsBeforeFinalBlock * itemHeight) + currentOffset;
|
| 298 |
+
|
| 299 |
+
// 4. Set the transition to none, snap to the alignment point
|
| 300 |
+
slotTrack.style.transition = 'none';
|
| 301 |
+
slotTrack.style.transform = `translateY(${snapPoint}px)`;
|
| 302 |
+
|
| 303 |
+
// 5. Apply the smooth deceleration transition to the final position:
|
| 304 |
+
// Use requestAnimationFrame to ensure the browser processes the 'transition: none' and 'snap' before starting the new transition.
|
| 305 |
+
requestAnimationFrame(() => {
|
| 306 |
+
// Set the slow, smooth transition curve
|
| 307 |
+
slotTrack.style.transition = `transform ${slowdownDuration}ms cubic-bezier(0.1, 0.7, 0.1, 1)`;
|
| 308 |
+
// Move to the final winning position
|
| 309 |
+
slotTrack.style.transform = `translateY(${finalTargetY}px)`;
|
| 310 |
+
});
|
| 311 |
+
|
| 312 |
+
// 6. Wait for the CSS transition to complete
|
| 313 |
+
setTimeout(() => {
|
| 314 |
+
declareWinner(winnerData.id, winnerData.index, totalItemsBeforeFinalBlock);
|
| 315 |
+
}, slowdownDuration + 100);
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
/**
|
| 321 |
+
* Starts the drawing process (slot machine spin).
|
| 322 |
+
*/
|
| 323 |
+
function startDraw() {
|
| 324 |
+
if (isDrawing || participants.length === 0) return;
|
| 325 |
+
|
| 326 |
+
// Remove previous highlight
|
| 327 |
+
slotTrack.querySelectorAll('.winner-highlight').forEach(el => el.classList.remove('winner-highlight'));
|
| 328 |
+
|
| 329 |
+
// Reset state and UI
|
| 330 |
+
isDrawing = true;
|
| 331 |
+
startButton.disabled = true;
|
| 332 |
+
startButton.textContent = '抽獎進行中...';
|
| 333 |
+
winnerOutput.classList.remove('scale-100');
|
| 334 |
+
winnerOutput.classList.add('scale-0');
|
| 335 |
+
|
| 336 |
+
// Reset position and start JS ANIMATION
|
| 337 |
+
slotTrack.style.transition = 'none'; // Ensure no CSS transitions interfere
|
| 338 |
+
currentY = 0;
|
| 339 |
+
slotTrack.style.transform = 'translateY(0px)';
|
| 340 |
+
|
| 341 |
+
let finalWinnerId = cheatingNumber ? parseInt(cheatingNumber, 10) : null;
|
| 342 |
+
let winnerIndex = -1;
|
| 343 |
+
|
| 344 |
+
// Cheating logic (finding the target winner)
|
| 345 |
+
if (finalWinnerId) {
|
| 346 |
+
winnerIndex = participants.findIndex(p => p.id === finalWinnerId);
|
| 347 |
+
if (winnerIndex === -1) {
|
| 348 |
+
finalWinnerId = null;
|
| 349 |
+
showStatus(`⚠️ 錯誤: 指定號碼 ${cheatingNumber} 不存在。執行隨機抽獎。`, 'bg-red-100 text-red-700', fastSpinDuration + slowdownDuration);
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
if (!finalWinnerId) {
|
| 354 |
+
// Random pick
|
| 355 |
+
const randomIndex = Math.floor(Math.random() * participants.length);
|
| 356 |
+
finalWinnerId = participants[randomIndex].id;
|
| 357 |
+
winnerIndex = randomIndex;
|
| 358 |
+
showStatus(`抽獎進行中...`, 'bg-blue-100 text-blue-700', fastSpinDuration + slowdownDuration);
|
| 359 |
+
} else {
|
| 360 |
+
showStatus(`抽獎進行中...`, 'bg-green-100 text-green-700', fastSpinDuration + slowdownDuration);
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
const initialSpeed = 150; // Constant speed for the fast spin (pixels per frame)
|
| 364 |
+
const startTime = performance.now();
|
| 365 |
+
|
| 366 |
+
const winnerData = {
|
| 367 |
+
id: finalWinnerId,
|
| 368 |
+
index: winnerIndex,
|
| 369 |
+
duration: fastSpinDuration,
|
| 370 |
+
startTime: startTime
|
| 371 |
+
};
|
| 372 |
+
|
| 373 |
+
animateSpin(winnerData, initialSpeed); // Start the smooth JS spin
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
/**
|
| 377 |
+
* Stops the spin animation and snaps to the winner.
|
| 378 |
+
* @param {number} finalWinnerId - The ID of the winner.
|
| 379 |
+
* @param {number} winnerIndex - The index of the winner in the participants array (0 to N-1).
|
| 380 |
+
* @param {number} totalItemsBeforeFinalBlock - The count of items before the final block.
|
| 381 |
+
*/
|
| 382 |
+
function declareWinner(finalWinnerId, winnerIndex, totalItemsBeforeFinalBlock) {
|
| 383 |
+
isDrawing = false;
|
| 384 |
+
|
| 385 |
+
const winner = participants.find(p => p.id === finalWinnerId);
|
| 386 |
+
|
| 387 |
+
if (!winner) {
|
| 388 |
+
winnerResult.textContent = '查無此號碼';
|
| 389 |
+
winnerOutput.classList.add('scale-100');
|
| 390 |
+
showStatus('抽獎結果無效,請檢查名單。', 'bg-red-100 text-red-700');
|
| 391 |
+
return;
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
// Highlight the winning element in the final block
|
| 395 |
+
const finalWinnerItem = slotItems[totalItemsBeforeFinalBlock + winnerIndex];
|
| 396 |
+
if (finalWinnerItem) {
|
| 397 |
+
finalWinnerItem.element.classList.add('winner-highlight');
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
// Display winner
|
| 401 |
+
winnerResult.textContent = `${winner.id} - ${winner.name}`;
|
| 402 |
+
winnerOutput.classList.add('scale-100');
|
| 403 |
+
showStatus(`已抽出幸運兒: ${winner.name}`, 'bg-purple-100 text-purple-700', 5000);
|
| 404 |
+
|
| 405 |
+
cheatingNumber = null;
|
| 406 |
+
startButton.disabled = false;
|
| 407 |
+
startButton.textContent = '重新抽獎';
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
/**
|
| 411 |
+
* Shows a temporary status message.
|
| 412 |
+
* @param {string} msg - The message to display.
|
| 413 |
+
* @param {string} className - Tailwind class names for styling.
|
| 414 |
+
* @param {number} duration - Duration in milliseconds.
|
| 415 |
+
*/
|
| 416 |
+
function showStatus(msg, className, duration = 3000) {
|
| 417 |
+
statusMessage.textContent = msg;
|
| 418 |
+
statusMessage.className = `p-3 rounded-lg text-sm mb-4 ${className}`;
|
| 419 |
+
statusMessage.classList.remove('hidden');
|
| 420 |
+
setTimeout(() => {
|
| 421 |
+
statusMessage.classList.add('hidden');
|
| 422 |
+
}, duration);
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
// --- Initialization ---
|
| 426 |
+
|
| 427 |
+
window.onload = () => {
|
| 428 |
+
// Add initial mock data
|
| 429 |
+
participantsInput.value = `1 艾倫
|
| 430 |
+
2 貝拉
|
| 431 |
+
3 查理
|
| 432 |
+
4 戴安
|
| 433 |
+
5 艾迪
|
| 434 |
+
6 芬妮
|
| 435 |
+
7 蓋瑞
|
| 436 |
+
8 海倫
|
| 437 |
+
9 伊恩
|
| 438 |
+
10 珍妮
|
| 439 |
+
11 凱文
|
| 440 |
+
12 莉莉`;
|
| 441 |
+
|
| 442 |
+
loadParticipants();
|
| 443 |
+
document.addEventListener('keydown', handleKeydown);
|
| 444 |
+
};
|
| 445 |
+
</script>
|
| 446 |
+
|
| 447 |
+
</body>
|
| 448 |
+
</html>
|