HTML實(shí)現(xiàn)寶箱抽獎代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>寶箱抽獎</title> <style> body{ background-color: #F8F8F8; text-align: center; } .box{ width: 200px; height: 200px; background-color: yellow; margin: 50px auto; position: relative; } .box:before{ content: ""; display: block; width: 100%; height: 60%; position: absolute; left: 0; top: 0; background-color: #F5A623; border-radius: 10px; transform: skew(45deg); transform-origin: left top; } .box:after{ content: ""; display: block; width: 100%; height: 60%; position: absolute; left: 0; bottom: 0; background-color: #F5A623; border-radius: 10px; transform: skew(-45deg); transform-origin: left bottom; } .btn{ display: inline-block; width: 100px; height: 30px; background-color: #90B44B; color: #fff; text-align: center; line-height: 30px; border-radius: 20px; cursor: pointer; } .prize{ position: absolute; left: 50%; top: 30%; transform: translate(-50%,-50%); font-size: 30px; color: #fff; text-shadow: 1px 1px 5px #000; z-index: 1; display: none; } </style> </head> <body> <div class="box"> <div class="prize">恭喜您獲得iPhone 12!</div> </div> <div class="btn" id="btn">開始抽獎</div> <script> var btn = document.getElementById("btn"); btn.onclick = function(){ var prizeBox = ["一等獎","二等獎","三等獎","安慰獎","iPhone 12"]; var index = Math.floor(Math.random() * prizeBox.length); var prize = document.querySelector(".prize"); prize.style.display = "block"; prize.innerHTML = "恭喜您獲得" + prizeBox[index] + "!"; }; </script> </body> </html>