使用js完成抽奖项目 效果和内容自定义,可以模仿游戏抽奖页面
html
<!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>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
color: #fff;
}
.container {
max-width: 900px;
width: 100%;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
padding: 30px;
text-align: center;
}
h1 {
font-size: 2.8rem;
margin-bottom: 20px;
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
color: #FFD700;
}
.subtitle {
font-size: 1.2rem;
margin-bottom: 40px;
color: rgba(255, 255, 255, 0.9);
}
.wheel-container {
position: relative;
width: 400px;
height: 400px;
margin: 0 auto 40px;
}
#wheel {
width: 100%;
height: 100%;
border-radius: 50%;
position: relative;
overflow: hidden;
transition: transform 4s cubic-bezier(0.17, 0.67, 0.13, 0.99);
box-shadow: 0 0 0 8px #e23232, 0 0 0 16px #f5d020;
background: #fff;
}
.wheel-section {
position: absolute;
width: 50%;
height: 50%;
transform-origin: bottom right;
left: 0;
top: 0;
display: flex;
align-items: center;
justify-content: center;
padding-left: 60px;
padding-bottom: 20px;
font-weight: bold;
color: #333;
text-shadow: 0 1px 1px rgba(255, 255, 255, 0.5);
}
.wheel-section:nth-child(2n) {
background: rgba(255, 255, 255, 0.8);
}
.wheel-section:nth-child(2n+1) {
background: rgba(230, 230, 230, 0.8);
}
.pointer {
position: absolute;
top: -40px;
left: 50%;
transform: translateX(-50%);
width: 30px;
height: 80px;
background: #ff3355;
clip-path: polygon(0 0, 100% 0, 50% 100%, 50% 100%);
z-index: 10;
filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.5));
}
.pointer:after {
content: '';
position: absolute;
top: -8px;
left: 50%;
transform: translateX(-50%);
width: 60px;
height: 60px;
background: #ffffff;
border-radius: 50%;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
}
.button-container {
margin: 20px 0 40px;
}
#spin-button {
padding: 16px 50px;
font-size: 1.4rem;
background: linear-gradient(to right, #ff416c, #ff4b2b);
color: white;
border: none;
border-radius: 50px;
cursor: pointer;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 2px;
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.5);
transition: all 0.3s ease;
}
#spin-button:hover {
transform: translateY(-3px);
box-shadow: 0 8px 20px rgba(255, 75, 43, 0.6);
}
#spin-button:disabled {
background: #999;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
.prizes-container {
background: rgba(0, 0, 0, 0.2);
border-radius: 15px;
padding: 20px;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 15px;
margin-bottom: 30px;
}
.prize-card {
background: rgba(255, 255, 255, 0.15);
border-radius: 12px;
padding: 15px;
transition: transform 0.3s ease;
}
.prize-card:hover {
transform: translateY(-5px);
background: rgba(255, 255, 255, 0.25);
}
.prize-card h3 {
font-size: 1.3rem;
margin-bottom: 8px;
color: #FFD700;
}
#result {
font-size: 1.5rem;
min-height: 36px;
margin: 20px 0;
color: #FFD700;
font-weight: bold;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
.confetti {
position: absolute;
width: 10px;
height: 10px;
background-color: #f00;
animation: confetti-fall 5s ease-in-out;
opacity: 0;
}
@keyframes confetti-fall {
0% {
transform: translateY(-100px) rotate(0deg);
opacity: 1;
}
100% {
transform: translateY(100vh) rotate(720deg);
opacity: 0;
}
}
@media (max-width: 700px) {
.wheel-container {
width: 420px;
height: 300px;
}
.wheel-section {
padding-left: 45px;
padding-bottom: 10px;
font-size: 0.9rem;
}
h1 {
font-size: 2.2rem;
}
.prizes-container {
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
}
}
</style>
</head>
<body>
<div class="container">
<h1>幸运大转盘</h1>
<div class="subtitle">转动转盘,赢取超值大奖!每天可抽奖3次</div>
<div class="wheel-container">
<div class="pointer"></div>
<div id="wheel">
<!-- 转盘区域会通过JS动态生成 -->
</div>
</div>
<div class="button-container">
<p id="result">转盘已就绪,点击开始抽奖!</p>
<button id="spin-button">开始抽奖</button>
</div>
<div class="prizes-container">
<div class="prize-card">
<h3>特等奖</h3>
<p>iPhone 15 Pro</p>
</div>
<div class="prize-card">
<h3>一等奖</h3>
<p>任天堂 Switch</p>
</div>
<div class="prize-card">
<h3>二等奖</h3>
<p>AirPods Pro</p>
</div>
<div class="prize-card">
<h3>三等奖</h3>
<p>200元现金</p>
</div>
<div class="prize-card">
<h3>幸运奖</h3>
<p>50元优惠券</p>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
javascript
// script.js
document.addEventListener('DOMContentLoaded', () => {
// 奖项配置:奖项名称、背景色、权重(权重值越大抽中几率越小)
const prizes = [
{ name: "谢谢参与", color: "#f8f9fa", weight: 4 },
{ name: "50元优惠券", color: "#ffdd33", weight: 3 },
{ name: "100元现金", color: "#4caf50", weight: 2 },
{ name: "AirPods Pro", color: "#03a9f4", weight: 2 },
{ name: "任天堂 Switch", color: "#9c27b0", weight: 1 },
{ name: "iPhone 15 Pro", color: "#ff5722", weight: 1 },
{ name: "300元现金", color: "#ff9800", weight: 2 },
{ name: "200元现金", color: "#8bc34a", weight: 2 }
];
const wheel = document.getElementById('wheel');
const spinButton = document.getElementById('spin-button');
const resultEl = document.getElementById('result');
let canSpin = true;
let attempts = 3; // 初始抽奖次数
// 初始化转盘
function initWheel() {
wheel.innerHTML = '';
const sliceAngle = 360 / prizes.length;
prizes.forEach((prize, i) => {
const section = document.createElement('div');
section.className = 'wheel-section';
section.textContent = prize.name;
section.style.backgroundColor = prize.color;
section.style.transform = `rotate(${i * sliceAngle}deg)`;
wheel.appendChild(section);
});
}
// 随机选择奖项(带权重系统)
function getRandomPrize() {
// 计算总权重
const totalWeight = prizes.reduce((sum, prize) => sum + prize.weight, 0);
// 生成随机数
const randomNum = Math.random() * totalWeight;
// 确定奖项
let weightSum = 0;
for (const prize of prizes) {
weightSum += prize.weight;
if (randomNum <= weightSum) {
return prize;
}
}
return prizes[0]; // 默认值
}
// 创建彩色纸屑特效
function createConfetti() {
const colors = ['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4CAF50', '#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800', '#FF5722'];
for (let i = 0; i < 150; i++) {
const confetti = document.createElement('div');
confetti.className = 'confetti';
confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
confetti.style.left = `${Math.random() * 100}%`;
confetti.style.animationDuration = `${Math.random() * 3 + 2}s`;
confetti.style.width = `${Math.random() * 10 + 5}px`;
confetti.style.height = `${Math.random() * 10 + 5}px`;
document.body.appendChild(confetti);
// 结束后清理
setTimeout(() => {
confetti.remove();
}, 5000);
}
}
// 旋转转盘
function spinWheel() {
if (!canSpin) return;
canSpin = false;
spinButton.disabled = true;
resultEl.textContent = "转盘旋转中...";
// 获取随机奖项
const prize = getRandomPrize();
// 每次旋转至少5圈(1800度)再加奖项位置
const sliceAngle = 360 / prizes.length;
const prizeIndex = prizes.findIndex(p => p.name === prize.name);
const rotation = 1800 + (prizeIndex * sliceAngle);
// 设置转盘旋转
wheel.style.transform = `rotate(${rotation}deg)`;
// 显示结果
setTimeout(() => {
attempts--;
spinButton.textContent = `开始抽奖 (${attempts}次机会)`;
resultEl.innerHTML = `恭喜您获得: <strong style="color:#ffdd00; font-size:1.6rem;">${prize.name}</strong>`;
createConfetti();
// 恢复按钮状态(如果还有机会)
if (attempts > 0) {
spinButton.disabled = false;
canSpin = true;
} else {
resultEl.innerHTML += "<br>今日抽奖机会已用完,明天再来吧!";
}
}, 4000);
}
// 事件监听
spinButton.addEventListener('click', spinWheel);
// 初始化
initWheel();
spinButton.textContent = `开始抽奖 (${attempts}次机会)`;
});

