帮我用HTML实现一个1到400随机抽取号数的网页抽奖器,有开始抽奖和重置的按钮,每次抽取一个号数,抽取时有随机数字滚动的动画,抽取号数不重复,要有已经抽取过的号数的显示区域,有美观的界面
时间: 2023-07-15 08:12:50 浏览: 160
好的,我可以帮你实现这个网页抽奖器。下面是代码实现:
```
<!DOCTYPE html>
<html>
<head>
<title>抽奖器</title>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
h1 {
color: #333;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
margin-top: 20px;
padding: 10px;
background-color: #fff;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
border-radius: 10px;
max-width: 500px;
width: 100%;
}
.number {
display: flex;
align-items: center;
justify-content: center;
font-size: 40px;
font-weight: bold;
color: #333;
background-color: #eee;
border-radius: 10px;
height: 80px;
width: 80px;
margin: 10px;
transition: all 0.2s ease-in-out;
cursor: pointer;
}
.number:hover {
transform: scale(1.1);
background-color: #ddd;
}
.selected {
background-color: #00bfff;
color: #fff;
}
.button-container {
display: flex;
align-items: center;
justify-content: center;
margin-top: 20px;
}
button {
background-color: #00bfff;
color: #fff;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 18px;
font-weight: bold;
margin: 0 10px;
cursor: pointer;
transition: all 0.2s ease-in-out;
}
button:hover {
background-color: #0080ff;
}
.result-container {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
margin-top: 20px;
padding: 10px;
background-color: #fff;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
border-radius: 10px;
max-width: 500px;
width: 100%;
}
.result {
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
font-weight: bold;
color: #333;
background-color: #eee;
border-radius: 10px;
height: 40px;
width: 40px;
margin: 5px;
}
</style>
</head>
<body>
<h1>抽奖器</h1>
<div class="container">
<script type="text/javascript">
var numbers = [];
var selectedNumbers = [];
var intervalId;
for (var i = 1; i <= 400; i++) {
numbers.push(i);
}
function selectNumber() {
if (numbers.length === 0) {
alert('所有号数已经抽取完毕!');
return;
}
var index = Math.floor(Math.random() * numbers.length);
var number = numbers[index];
selectedNumbers.push(number);
numbers.splice(index, 1);
var numberElement = document.getElementById('number-' + number);
numberElement.classList.add('selected');
var resultElement = document.createElement('div');
resultElement.classList.add('result');
resultElement.innerText = number;
document.getElementById('result-container').appendChild(resultElement);
}
function start() {
if (intervalId) {
return;
}
intervalId = setInterval(function() {
var numberElements = document.getElementsByClassName('number');
for (var i = 0; i < numberElements.length; i++) {
numberElements[i].classList.remove('selected');
}
selectNumber();
}, 100);
}
function stop() {
if (!intervalId) {
return;
}
clearInterval(intervalId);
intervalId = null;
}
function reset() {
if (intervalId) {
stop();
}
numbers = [];
selectedNumbers = [];
for (var i = 1; i <= 400; i++) {
numbers.push(i);
var numberElement = document.createElement('div');
numberElement.classList.add('number');
numberElement.id = 'number-' + i;
numberElement.innerText = i;
document.getElementById('container').appendChild(numberElement);
}
var resultElements = document.getElementsByClassName('result');
while (resultElements.length > 0) {
resultElements[0].parentNode.removeChild(resultElements[0]);
}
}
</script>
<div id="container"></div>
</div>
<div class="button-container">
<button onclick="start()">开始抽奖</button>
<button onclick="stop()">停止抽奖</button>
<button onclick="reset()">重置</button>
</div>
<div class="result-container" id="result-container"></div>
</body>
</html>
```
这段代码实现了一个抽奖器,包括1到400的号数,开始抽奖和停止抽奖的按钮,已经抽取过的号数的显示区域,以及美观的界面。具体实现细节请看代码注释。
阅读全文