随机抽奖 相当于创建9个div,使用绝对定位给他们进行固定 生成一个随机数,选到一个中奖的号码 为了能在转盘上多转几圈,可以扩大随机数的生成范围,到时候直接取模就好了 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #box { width: 300px; height: 300px; position: relative; margin: 0 auto; box-shadow: 0 0 15px rgba(0, 0, 0, 0.4); } #box div { width: 98px; height: 98px; line-height: 98px; border: 1px solid red; text-align: center; position: absolute; } #box div:nth-child(2) { left: 100px; } #box div:nth-child(3) { left: 200px; } #box div:nth-child(4) { left: 200px; top: 100px; } #box div:nth-child(5) { left: 200px; top: 200px; } #box div:nth-child(6) { left: 100px; top: 200px; } #box div:nth-child(7) { top: 200px; } #box div:nth-child(8) { top: 100px; } #box input { position: absolute; width: 98px; height: 98px; left: 101px; top: 101px; border: 1px solid red; } .red { background-color: red; } </style></head><body> <div id="box"> <div>一等奖</div> <div>二等奖</div> <div>三等奖</div> <div>四等奖</div> <div>五等奖</div> <div>六等奖</div> <div>七等奖</div> <div>八等奖</div> <input type="button" id="startBtn" value="开始"> </div> <h2></h2></body><script type="text/javascript"> var box = document.getElementById("box"); var allDiv = document.getElementsByTagName("div"); var startBtn = document.getElementById("startBtn") var scoreH2 = document.getElementsByTagName("h2")[0]; // console.log(allDiv); var timer = null; var flag = false; startBtn.onclick = function () { if (flag) { return; } flag = true; var idx = Math.ceil(Math.random() * 24); console.log(idx); var k = 0; timer = setInterval(function () { for (var i = 0; i < allDiv.length; i++) { allDiv[i].className = ""; } allDiv[k % 8].className = "red"; if (k === idx) { clearTimeout(timer); scoreH2.innerText = "恭喜中奖啦!"; flag = false; } k++; }, 100) }</script></html>