您好,登錄后才能下訂單哦!
這篇文章主要介紹了JS如何實現簡單九宮格抽獎的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇JS如何實現簡單九宮格抽獎文章都會有所收獲,下面我們一起來看看吧。
HTML文件:
<body> <div class="box"> <ul id="jiangPin"> <li><a href="javascript:viod(0);" ><span>獎品 1</span></a></li> <li><a href="javascript:viod(0);" ><span>獎品 2</span></a></li> <li><a href="javascript:viod(0);" ><span>獎品 3</span></a></li> <li><a href="javascript:viod(0);" ><span>獎品 4</span></a></li> <li><a href="javascript:viod(0);" ><span>獎品 5</span></a></li> <li><a href="javascript:viod(0);" ><span>獎品 6</span></a></li> <li><a href="javascript:viod(0);" ><span>獎品 7</span></a></li> <li><a href="javascript:viod(0);" ><span>獎品 8</span></a></li> </ul> <button type="button" class="btn" id="beginBtn">點擊開始</button> <div class="tishi" id="tishi"> <span>恭喜您獲得:</span> <p></p> <button>確定</button> </div> </div> </body>
通過position定位來固定獎盤各個板塊的位置,將有獎品的8個塊分散在九宮格的邊緣,開始按鈕在九宮格正中間,將彈出的提示框放于整個獎盤的上層顯示,初始將其隱藏。
CSS文件:
.box{ width: 450px; height: 450px; background-color: #9a6e3a; border: 2px solid #990055; position: relative; z-index: 10; } .box>ul>li{ position: absolute; width: 148px; height: 148px; border: 1px #222222 solid; background-color: #ad889d; } .box>ul>li>a{ display: block; color: #fff; font-size: 30px; text-align: center; line-height: 148px; } /* 開始按鈕 */ .btn{ position: absolute; top: 150px; left: 150px; width: 150px; height: 150px; border: 1px #222222 solid; background-color: #7d53c7; outline: none; cursor: pointer; color: #fff; font-size: 25px; transition: all 0.5s; z-index: 20; } .btn:hover{ background-color: #df04ff; font-size: 30px; } .btn:active{ background-color: #7d53c7; } .box>ul>li.on{ background-color: #f00; } /* 開始按鈕 end */ /* 獎品定位 */ .box>ul>li:nth-child(1){ top: 0; left: 0; } .box>ul>li:nth-child(2){ top: 0; left: 150px; } .box>ul>li:nth-child(3){ top: 0; left: 300px; } .box>ul>li:nth-child(4){ top: 150px; left: 300px; } .box>ul>li:nth-child(5){ top: 300px; left: 300px; } .box>ul>li:nth-child(6){ top: 300px; left: 150px; } .box>ul>li:nth-child(7){ top: 300px; left: 0; } .box>ul>li:nth-child(8){ top: 150px; left: 0; } /* 獎品定位 end */ /* 提示框 */ .tishi{ width: 300px; height: 146px; border: 2px #990055 solid; background: #92EC1F; border-radius: 20px; position: absolute; left: 50%; top: 50%; margin-top: -75px; margin-left: -150px; z-index: 30; text-align: center; font-size: 25px; font-weight: bold; display: none; animation: tishiAni 0.5s both; transition: all 0.5s; } .tishi>p{ color: red; font-size: 28px; margin-top: 20px; } .tishi>button{ width: 60px; height: 30px; border:none; outline: none; cursor: pointer; position: absolute; right: 30px; bottom: 20px; } @keyframes tishiAni { 0%{ opacity: 0; } 100%{ opacity: 1; } } /* 提示框 end */
通過計時器來實現獎品亮塊的循環
JavaScript文件:
{ let jiangPinChi = [ "獎品1", "獎品2", "獎品3", "獎品4", "獎品5", "獎品6", "獎品7", "獎品8", ]; let index = 0; let times = Math.round( Math.random()*20 ) +20 ; let jiangPin = document.getElementById("jiangPin"); let beginBtn = document.getElementById("beginBtn"); let tishi = document.getElementById("tishi"); let cont = tishi.children; let jPLi = jiangPin.children; let liBro = function (tag) { let fat = tag.parentNode; let tagLi = fat.children; let othLis = []; for (let jPLi of tagLi) { if (jPLi === tag) { continue; } othLis.push(jPLi); } return othLis; }; let showing = function( index ) { let othLis = liBro( jPLi[index] ); for( let i = 0; i<=othLis.length-1 ; i++ ){ othLis[i].classList.remove("on"); } // othLis.forEach(function( value,i ){ // value.classList.remove("on"); // }); jPLi[index].classList.add("on"); }; let changeFun = function( event ){ let myset = setInterval(function(){ index++; times--; if( index > jPLi.length-1 ){ index = 0; } showing( index ); if( times <= 0 ){ clearInterval(myset); times = Math.round( Math.random()*20 ) +20; tishi.style.display = "block"; cont[1].innerHTML = jiangPinChi[index]; } },100); }; beginBtn.addEventListener("click",changeFun); cont[2].addEventListener("click",function( event ){ tishi.style.display = "none"; }); }
關于“JS如何實現簡單九宮格抽獎”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“JS如何實現簡單九宮格抽獎”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。