You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
select_random/lottery.html

136 lines
5.2 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>抽奖页面</title>
<style>
body {
font-family: 'Arial', sans-serif;
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(135deg, #6e45e2, #88d3ce);
color: white;
transition: background-color 0.5s ease;
}
.container {
text-align: center;
background-color: rgba(255, 255, 255, 0.1);
padding: 30px;
border-radius: 15px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
opacity: 1;
transition: opacity 0.5s ease;
}
#pointsDisplay {
font-size: 1.5em;
margin-bottom: 20px;
}
button {
padding: 12px 30px;
font-size: 1.1em;
border: none;
border-radius: 50px;
background-color: #ff5722;
color: white;
cursor: pointer;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
margin: 10px;
}
button:hover {
background-color: #ff3d00;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}
#loading {
display: none;
font-size: 1.5em;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div id="pointsDisplay">当前积分: </div>
<button id="startLotteryButton">开始抽奖</button>
<button class="back-button" id="backButton">返回前一页面</button>
<div id="result"></div>
<div id="loading">正在加载,请稍候...</div>
</div>
<script>
const urlParams = new URLSearchParams(window.location.search);
let name = urlParams.get('name');
// 如果 URL 中没有 name 参数,尝试从 localStorage 获取
if (!name) {
name = localStorage.getItem('currentUserName');
}
const pointsDisplay = document.querySelector('#pointsDisplay');
const startLotteryButton = document.querySelector('#startLotteryButton');
const resultDisplay = document.querySelector('#result');
const backButton = document.getElementById('backButton');
const loadingIndicator = document.getElementById('loading');
// 睡眠函数,让页面跳转更自然
function sleep(d) {
return new Promise(resolve => setTimeout(resolve, d));
}
// 获取该用户的积分
let userPoints = parseInt(localStorage.getItem(name)) || 0;
pointsDisplay.textContent = `${name} 的当前积分: ${userPoints}`;
// 设置返回按钮的 URL
backButton.onclick = () => {
loadingIndicator.style.display = 'block'; // 显示加载提示
window.location.href = `choice.html?name=${encodeURIComponent(name)}`;
};
// 抽奖逻辑
startLotteryButton.addEventListener('click', async () => {
if (userPoints >= 20) {
userPoints -= 20; // 每次抽奖消耗 20 分
localStorage.setItem(name, userPoints); // 保存更新后的积分
pointsDisplay.textContent = `${name} 的当前积分: ${userPoints}`;
// 随机选择奖品
const prizes = ["跳过权", "再来一次", "什么也没有发生", "加30积分且跳过"];
const randomPrize = prizes[Math.floor(Math.random() * prizes.length)];
resultDisplay.textContent = `你赢得了: ${randomPrize}`;
// 等待 2 秒以展示结果
await sleep(2000);
loadingIndicator.style.display = 'block'; // 显示加载提示
if (randomPrize === '跳过权') {
await sleep(1000);
window.location.href = `select.html?name=${encodeURIComponent(name)}`;
} else if (randomPrize === '再来一次') {
userPoints += 20;
localStorage.setItem(name, userPoints);
} else if (randomPrize === '什么也没有发生') {
await sleep(1000);
window.location.href = `answer.html?name=${encodeURIComponent(name)}`;
} else if (randomPrize === '加30积分且跳过') {
userPoints += 30;
localStorage.setItem(name, userPoints);
await sleep(1000);
window.location.href = `select.html?name=${encodeURIComponent(name)}`;
}
} else {
resultDisplay.textContent = "积分不足,无法抽奖!";
loadingIndicator.style.display = 'block'; // 显示加载提示
await sleep(1000);
window.location.href = `answer.html?name=${encodeURIComponent(name)}`;
}
});
</script>
</body>
</html>