// 定义标志变量,用于跟踪动画是否正在运行 let isAnimating = false; // 最终停止位置的变量 var finalPosition; // 老虎机旋转速度 let rotationSpeed = 0.5; // 用于保存第一次的结果 let firstTimeResult = null; // 用于跟踪用户点击的次数 let clickCount = 0; // 为开始/停止按钮添加点击事件监听器 document.getElementById('toggleButton').addEventListener('click', function () { if (!isAnimating) { bool.start(); $('.jbBox').css('z-index', 3) // startAllAnimations(); // setTimeout(() => { // if (isAnimating) { // stopAllAnimationsSequentially() // isAnimating = false; // } // }, 2000); } else { stopAllAnimationsSequentially(); $('.jbBox').css('z-index', 2) bool.reset(); } isAnimating = !isAnimating; }); // 定义开始所有动画的函数 function startAllAnimations() { const backgroundMusic = document.getElementById('backgroundMusic'); backgroundMusic.play(); // 播放背景音乐 // 每次点击时都生成一个新的随机数 const randomNum = Math.floor(Math.random() * 17) + 1; // 假设有40个不同的位置/图像 // finalPosition = -(randomNum * 153); // 计算最终位置,这里的153需要根据实际的元素高度调整 finalPosition = randomNum; // 计算最终位置,这里的153需要根据实际的元素高度调整 // 应用动画到所有的老虎机数字容器 document.querySelectorAll('.TigerNumbers').forEach(tigerNumbers => { tigerNumbers.style.animation = `rollAnimation ${rotationSpeed}s infinite linear`; }); } // 定义顺序停止所有动画的函数 function stopAllAnimationsSequentially() { const tigerNumbersList = document.querySelectorAll('.TigerNumbers'); tigerNumbersList.forEach((tigerNumbers, index) => { setTimeout(() => { tigerNumbers.style.animation = 'none'; // 停止动画 tigerNumbers.style.top = `calc(-2.5rem + 0.24rem - ((2.5rem + 0.15rem) * (${finalPosition} - 1)))`; // 设置最终停止位置 if (index === tigerNumbersList.length - 1) { // 如果是最后一个动画 setTimeout(() => { showModalWithImage(tigerNumbers); // 显示带有图片的模态框 }, 1000); // 延迟1秒显示模态框 } }, index * 2000); // 每个动画停止的间隔时间 }); } // 定义显示带有图片的模态框的函数 function showModalWithImage(tigerNumbers) { const imgSrc = tigerNumbers.children[finalPosition].querySelector('img').src; document.getElementById('modalContent').querySelector('img').src = imgSrc; document.getElementById('modal').style.display = 'block'; } // 为关闭按钮添加点击事件监听器,用于关闭模态框并停止背景音乐 document.getElementById('closeButton').addEventListener('click', function () { const backgroundMusic = document.getElementById('backgroundMusic'); backgroundMusic.pause(); // 停止播放背景音乐 backgroundMusic.currentTime = 0; // 重置音乐到开始位置 document.getElementById('modal').style.display = 'none'; // 隐藏模态框 });