帮我写一个网页,要求实现转盘抽奖功能
时间: 2024-02-13 16:04:25 浏览: 73
好的,下面是一个基于 HTML、CSS 和 JavaScript 实现的转盘抽奖网页,你可以参考以下代码实现:
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>转盘抽奖</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<div class="turntable">
<div class="pointer"></div>
<ul class="prize-list">
<li>奖品1</li>
<li>奖品2</li>
<li>奖品3</li>
<li>奖品4</li>
<li>奖品5</li>
<li>奖品6</li>
<li>奖品7</li>
<li>奖品8</li>
</ul>
</div>
<button class="btn" onclick="start()">开始抽奖</button>
</div>
<script src="jquery-3.5.1.min.js"></script>
<script src="script.js"></script>
</body>
</html>
```
CSS 代码:
```css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.turntable {
position: relative;
width: 300px;
height: 300px;
}
.pointer {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 100px;
background-image: url(pointer.png);
background-size: cover;
}
.prize-list {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
list-style: none;
}
.prize-list li {
display: inline-block;
margin: 0 10px;
padding: 20px;
background-color: #f1c40f;
border-radius: 50%;
font-size: 16px;
font-weight: bold;
}
.btn {
margin-top: 20px;
padding: 10px 20px;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
```
JavaScript 代码:
```javascript
// 奖品列表
var prizes = [
"奖品1",
"奖品2",
"奖品3",
"奖品4",
"奖品5",
"奖品6",
"奖品7",
"奖品8"
];
// 转盘旋转角度
var angles = [0, 45, 90, 135, 180, 225, 270, 315];
// 是否正在抽奖
var isRunning = false;
// 开始抽奖
function start() {
if (isRunning) {
return;
}
isRunning = true;
// 随机生成旋转角度
var index = Math.floor(Math.random() * prizes.length);
var angle = angles[index] + Math.floor(Math.random() * 45);
// 转盘旋转
$(".turntable").rotate({
angle: 0,
animateTo: angle + 1440,
duration: 3000,
easing: $.easing.easeOutSine,
callback: function() {
// 抽奖结束,显示奖品
alert(prizes[index]);
isRunning = false;
}
});
}
// jQuery 插件,实现元素旋转效果
$.fn.rotate = function(options) {
var defaults = {
angle: 0,
animateTo: 0,
duration: 1000,
easing: $.easing.easeInOutExpo,
callback: function() {}
};
var settings = $.extend(defaults, options);
return this.each(function() {
var $elem = $(this);
$({deg: settings.angle}).animate({deg: settings.animateTo}, {
duration: settings.duration,
easing: settings.easing,
step: function(now) {
$elem.css({
transform: "rotate(" + now + "deg)"
});
},
complete: function() {
settings.callback.call(this);
}
});
});
};
```
在上述代码中,奖品列表和转盘旋转角度可以根据实际情况进行修改。其中,`jquery-3.5.1.min.js` 和 `pointer.png` 文件需要下载并放置在同级目录下。
阅读全文