<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>拯救选择困难症</title>
<style>
:root {
--primary-color: #6C5CE7;
--danger-color: #ff4757;
--background: #F8F9FA;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Segoe UI', system-ui, sans-serif;
background: var(--background);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
.container {
width: 100%;
max-width: 1200px;
background: white;
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
padding: 30px;
margin-top: 20px;
}
h1 {
text-align: center;
color: var(--primary-color);
margin-bottom: 30px;
font-size: 2.5rem;
}
.input-group {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-bottom: 25px;
}
.input-item {
position: relative;
flex: 1 1 100%;
min-width: auto;
margin-bottom: 10px;
}
.delete-btn {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
background: none;
color: #999;
border: none;
font-size: 20px;
cursor: pointer;
z-index: 2;
width: 30px;
height: 30px;
display: none;
align-items: center;
justify-content: center;
opacity: 0.7;
transition: opacity 0.3s;
}
.input-item:hover .delete-btn,
.input-item:focus-within .delete-btn {
opacity: 1;
}
input {
width: 100%;
padding: 15px 40px 15px 15px;
border: 2px solid #ddd;
border-radius: 12px;
font-size: 16px;
transition: all 0.3s;
background-clip: padding-box;
}
input:focus {
border-color: var(--primary-color);
outline: none;
box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.2);
}
.button-group {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin: 20px 0;
justify-content: center;
}
button {
padding: 12px 30px;
border: none;
border-radius: 10px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
flex: 1 1 200px;
text-align: center;
}
.primary-btn {
background: var(--primary-color);
color: white;
}
.secondary-btn {
background: #f1f2f5;
color: #464646;
}
.danger-btn {
background: var(--danger-color);
color: white;
}
.options-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin: 30px 0;
}
@media (min-width: 768px) {
.options-grid {
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
}
.option-box {
background: #F3F4F6;
padding: 25px;
border-radius: 15px;
text-align: center;
font-size: 18px;
transition: all 0.3s;
min-height: 100px;
display: flex;
align-items: center;
justify-content: center;
}
.highlight {
background: var(--primary-color);
color: white;
transform: scale(1.05);
box-shadow: 0 5px 15px rgba(108, 92, 231, 0.3);
}
#countdown {
text-align: center;
font-size: 24px;
color: var(--primary-color);
margin: 20px 0;
display: none;
}
#result {
display: none;
text-align: center;
font-size: 32px; /* 调整字号 */
color: var(--primary-color);
padding: 30px;
background: #F3F4F6;
border-radius: 15px;
margin: 20px 0;
animation: fadeIn 0.5s;
}
/* 修改后的结果样式 */
#result:before {
content: "最终选择";
display: block;
font-size: 24px;
color: var(--primary-color);
margin-bottom: 10px;
text-align: center;
}
#result span {
font-size: 32px;
color: red;
display: block;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 768px) {
button {
flex: 1 1 100%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>拯救选择困难症</h1>
<div class="input-group">
<div class="input-item">
<input type="text" class="option-input" placeholder="输入选项 1">
<button class="delete-btn">×</button>
</div>
<div class="input-item">
<input type="text" class="option-input" placeholder="输入选项 2">
<button class="delete-btn">×</button>
</div>
</div>
<div class="button-group">
<button class="secondary-btn">
<span>➕</span> 新增
</button>
<button class="primary-btn">
<span>🎲</span> 开始
</button>
<button class="danger-btn">
<span>🔄</span> 重置
</button>
</div>
<div class="lottery-section" style="display: none;">
<div class="options-grid" id="optionsContainer"></div>
<div id="countdown">正在选择中... 5秒</div>
<div id="result"></div>
<div class="button-group" id="backBtn">
<button class="primary-btn">
<span>←</span> 返回重新选择
</button>
</div>
</div>
</div>
<script>
// 初始化删除按钮状态
document.addEventListener('DOMContentLoaded', () => {
updateDeleteButtons();
});
// 添加新选项
function addOption() {
const inputGroup = document.querySelector('.input-group');
const newInput = document.createElement('div');
newInput.className = 'input-item';
newInput.innerHTML = `
<input type="text" class="option-input"
placeholder="输入选项 ${document.querySelectorAll('.option-input').length + 1}">
<button class="delete-btn">×</button>
`;
inputGroup.appendChild(newInput);
updateDeleteButtons();
}
// 删除选项
function removeOption(inputItem) {
if (document.querySelectorAll('.input-item').length > 2) {
inputItem.remove();
updatePlaceholders();
updateDeleteButtons();
}
}
// 更新删除按钮显示状态
function updateDeleteButtons() {
const items = document.querySelectorAll('.input-item');
items.forEach((item) => {
const deleteBtn = item.querySelector('.delete-btn');
deleteBtn.style.display = items.length > 2 ? 'flex' : 'none';
});
}
// 更新placeholder序号
function updatePlaceholders() {
document.querySelectorAll('.option-input').forEach((input, index) => {
input.placeholder = `输入选项 ${index + 1}`;
});
}
// 重置所有
function resetAll() {
const inputGroup = document.querySelector('.input-group');
while (inputGroup.children.length > 2) {
inputGroup.removeChild(inputGroup.lastChild);
}
document.querySelectorAll('.option-input').forEach(input => {
input.value = '';
});
updatePlaceholders();
updateDeleteButtons();
// 隐藏抽奖界面
document.querySelector('.lottery-section').style.display = 'none';
document.getElementById('result').style.display = 'none';
document.getElementById('backBtn').style.display = 'none';
}
// 开始抽选
function startLottery() {
const options = Array.from(document.querySelectorAll('.option-input'))
.map(input => input.value.trim())
.filter(text => text);
if (options.length < 2) {
alert("请至少输入两个有效选项!");
return;
}
// 隐藏输入界面
document.querySelector('.input-group').style.display = 'none';
document.querySelector('.button-group').style.display = 'none';
const lotterySection = document.querySelector('.lottery-section');
lotterySection.style.display = 'block';
document.getElementById('countdown').style.display = 'block';
// 必须确保结果隐藏
document.getElementById('result').style.display = 'none';
// 生成选项框
const optionsContainer = document.getElementById('optionsContainer');
optionsContainer.innerHTML = options.map(opt => `
<div class="option-box">${opt}</div>
`).join('');
// 初始化变量
let seconds = 5;
const countdownElement = document.getElementById('countdown');
const resultElement = document.getElementById('result');
const optionBoxes = document.querySelectorAll('.option-box');
let winnerIndex = -1;
// 动画效果
let highlightInterval = setInterval(() => {
const randomIndex = Math.floor(Math.random() * optionBoxes.length);
optionBoxes.forEach(box => box.classList.remove('highlight'));
optionBoxes[randomIndex].classList.add('highlight');
winnerIndex = randomIndex;
}, 50);
// 倒计时
const timer = setInterval(() => {
seconds--;
countdownElement.textContent = `正在选择中... ${seconds}秒`;
if (seconds <= 0) {
clearInterval(highlightInterval);
clearInterval(timer);
showResult();
}
}, 1000);
function showResult() {
optionBoxes.forEach(box => box.classList.remove('highlight'));
if(winnerIndex >= 0) {
optionBoxes[winnerIndex].classList.add('highlight');
resultElement.textContent = `最终选择\n${options[winnerIndex]}`;
// 动态插入结果内容
resultElement.innerHTML = `<span>${options[winnerIndex]}</span>`;
}
countdownElement.style.display = 'none';
resultElement.style.display = 'block';
document.getElementById('backBtn').style.display = 'flex';
}
}
// 返回输入界面
function backToInput() {
document.querySelector('.lottery-section').style.display = 'none';
document.querySelector('.input-group').style.display = 'flex';
document.querySelector('.button-group').style.display = 'flex';
document.getElementById('backBtn').style.display = 'none';
document.getElementById('countdown').style.display = 'none';
document.getElementById('result').style.display = 'none';
// 重置选项框高亮
document.querySelectorAll('.option-box').forEach(box => {
box.classList.remove('highlight');
});
// 重置倒计时文本
document.getElementById('countdown').textContent = '正在选择中... 5秒';
}
// 其他改进功能(如选项验证、倒计时调整、结果分享、主题切换等)可以在此基础上继续扩展
</script>
</body>
</html>