Spaces:
Sleeping
Sleeping
File size: 10,302 Bytes
50408e8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>转盘抽奖</title>
<style>
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 100%;
background: #264563;
}
.luckdraw {
width: 300px;
height: 300px;
position: relative;
margin: 100px auto;
border-radius: 50%;
overflow: hidden;
}
.luckpanel {
position: relative;
width: 100%;
height: 100%;
transition: transform 3s ease-in-out;
}
#canvas {
position: relative;
width: 100%;
height: 100%;
}
#canvas .sector-item {
position: absolute;
top: 0;
left: 0;
width: 50%;
height: 50%;
transform-origin: right bottom;
overflow: hidden;
}
/* #canvas .sector-item:nth-child(odd) {
background: pink;
}
#canvas .sector-item:nth-child(even) {
background: skyblue;
} */
.prize {
position: absolute;
top: 0;
left: 50%;
width: 50%;
height: 50%;
transform: translateX(-50%);
}
.prize-item {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform-origin: center bottom;
}
.prize-item__name {
position: absolute;
top: 20px;
left: 10px;
width: calc(100% - 20px);
font-size: 12px;
text-align: center;
color: #ff572f;
}
.prize-item__img {
position: absolute;
top: 50px;
left: calc(50% - 30px /2);
width: 30px;
height: 30px;
}
.prize-item__img img {
width: 100%;
height: 100%;
vertical-align: bottom;
}
/* 抽奖 */
.pointer {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 90px;
height: 90px;
background-color: orange;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
font-weight: bold;
cursor: pointer;
user-select: none;
}
.pointer::after {
content: '';
position: absolute;
top: -70px;
border: 40px solid orange;
border-left-width: 10px;
border-right-width: 10px;
border-left-color: transparent;
border-top-color: transparent;
border-right-color: transparent;
}
</style>
</head>
<body>
<div class="luckdraw">
<div class="luckpanel">
<div id="canvas"></div>
<div class="prize">
<!-- <div class="prize-item">
<div class="prize-item__name">奖品</div>
<div class="prize-item__img">
<img
src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.it168.com%2Fn%2F640x480%2F6%2F6414%2F6414197.jpg&refer=http%3A%2F%2Fimage.it168.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640667263&t=18bb80a694a5ff966713191b4a81745d"
alt="">
</div>
</div> -->
</div>
</div>
<div class="pointer">抽奖</div>
</div>
<script>
const consts = {
// 奖品清单
prizeList: [
// {
// prizeName: '休息多5分钟',
// // prizeImg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.it168.com%2Fn%2F640x480%2F6%2F6414%2F6414197.jpg&refer=http%3A%2F%2Fimage.it168.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640667263&t=18bb80a694a5ff966713191b4a81745d',
// count: 10,
// },
// {
// prizeName: '休息多10分钟',
// // prizeImg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.it168.com%2Fn%2F640x480%2F6%2F6414%2F6414197.jpg&refer=http%3A%2F%2Fimage.it168.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640667263&t=18bb80a694a5ff966713191b4a81745d',
// count: 5,
// },
{
prizeName: '托马斯1集',
// prizeImg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.it168.com%2Fn%2F640x480%2F6%2F6414%2F6414197.jpg&refer=http%3A%2F%2Fimage.it168.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640667263&t=18bb80a694a5ff966713191b4a81745d',
count: 6,
},
{
prizeName: '丁丁1集',
// prizeImg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.it168.com%2Fn%2F640x480%2F6%2F6414%2F6414197.jpg&refer=http%3A%2F%2Fimage.it168.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640667263&t=18bb80a694a5ff966713191b4a81745d',
count: 3,
},
{
prizeName: '小熊15分钟',
// prizeImg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.it168.com%2Fn%2F640x480%2F6%2F6414%2F6414197.jpg&refer=http%3A%2F%2Fimage.it168.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640667263&t=18bb80a694a5ff966713191b4a81745d',
count: 5,
},
{
prizeName: '读书写字画画10分钟',
// prizeImg: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.it168.com%2Fn%2F640x480%2F6%2F6414%2F6414197.jpg&refer=http%3A%2F%2Fimage.it168.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640667263&t=18bb80a694a5ff966713191b4a81745d',
count: 2,
}
],
// 每一块扇形的背景色
prizeBgColors: [
'rgb(255,231,149)',
'rgb(255,247,223)',
'rgb(255,231,149)',
'rgb(255,247,223)',
'rgb(255,231,149)',
'rgb(255,247,223)',
'rgb(255,231,149)',
'rgb(255,247,223)',
],
// 每一块扇形的外边框颜色
borderColor: '#ff9800'
}
const prizeNum = consts.prizeList.length
let itemAngle = 360 / prizeNum // 每个扇形区域的角度
const offsetAngle = itemAngle / 2
let isRotating = false
const circleCount = 3 // 旋转圈数
const rotateDuration = 3 // 持续时间
const panel = document.querySelector('.luckpanel')
// 画出扇形骨架
function drawPanel() {
let fragMent = document.createDocumentFragment()
consts.prizeList.forEach((item, index) => {
let itemDom = document.createElement('div')
itemDom.setAttribute('class', 'sector-item')
itemDom.style.background = `${consts.prizeBgColors[index]}`
itemDom.style.borderBottom = `1px solid ${consts.borderColor}`
itemDom.style.transform = `rotate(${itemAngle * (index + 1)}deg) skewY(${90 - itemAngle}deg)`
fragMent.appendChild(itemDom)
})
document.getElementById('canvas').appendChild(fragMent)
}
function getPrizeItem({ name, src }) {
const el = document.createElement('div')
let tpl = ''
if(src){
tpl = `
<div class="prize-item">
<div class="prize-item__name">${name}</div>
<div class="prize-item__img">
<img
src="${src}"
alt="">
</div>
</div>
`
}
else{
tpl = `
<div class="prize-item">
<div class="prize-item__name">${name}</div>
<div class="prize-item__img">
</div>
</div>
`
}
el.innerHTML = tpl
return el.firstElementChild
}
// 填充奖品内容
function fillPrize() {
const container = document.querySelector('.prize')
consts.prizeList.forEach((item, i) => {
const el = getPrizeItem({
name: item.prizeName,
src: item.prizeImg
})
// 旋转
const currentAngle = itemAngle * i + offsetAngle
el.style.transform = `rotate(${currentAngle}deg)`
container.appendChild(el)
})
}
// 获得哪一个奖品
function getPrizeIndex(prizeList) {
// 中奖的是哪一个奖品
let index = 0
// 当前奖品总数量
let prizeTotalNum = 0
for (let i = 0; i < prizeList.length; i++) {
prizeTotalNum += prizeList[i].count
}
if (prizeTotalNum === 0) {
alert('奖品已抽完');
return index = -1
}
// 产生一个随机数 0-总数
let random = parseInt(Math.random() * prizeTotalNum)
// 当前奖品的概率区间
let currentWeight = 0
for (let i = 0; i < prizeList.length; i++) {
currentWeight += prizeList[i].count
if (random < currentWeight) {
index = i
prizeList[i].count--
break
}
}
return index
}
// 抽奖事件
function bindEvent() {
document.querySelector('.pointer').addEventListener('click', function () {
if (isRotating) {
return
} else {
isRotating = true
}
const index = getPrizeIndex(consts.prizeList)
console.log('index', index);
if (index === -1) {
isRotating = false
} else {
console.log('奖品名称', consts.prizeList[index].prizeName);
rotate(index)
}
})
}
// 旋转转盘
let statrtRotatAngle = 0
function rotate(index) {
// statrtRotatAngle % 360 上一次旋转到index的度数
// statrtRotatAngle - statrtRotatAngle % 360 得到一个度数为 360*n
// 旋转到索引index商品的度数
let angleToIndex = 360 - (offsetAngle + itemAngle * index)
const rotateAngle = statrtRotatAngle - statrtRotatAngle % 360 + circleCount * 360 + angleToIndex
statrtRotatAngle = rotateAngle
panel.style.transform = `rotate(${rotateAngle}deg)`
panel.style.transitionDuration = `${rotateDuration}s`
setTimeout(() => {
isRotating = false
}, 1000)
}
function init() {
// 画出扇形骨架
drawPanel()
// 填充奖品内容
fillPrize()
// 抽奖事件
bindEvent()
document.onkeydown = function (event) {
if(event.key === 'm'){
window.location.href = 'math.html'
}
}
}
document.addEventListener('DOMContentLoaded', init)
</script>
</body>
</html> |