佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > html5實現(xiàn)九宮格抽獎可固定抽中某項獎品

html5實現(xiàn)九宮格抽獎可固定抽中某項獎品

熱門標(biāo)簽:網(wǎng)站建設(shè) 阿里云 美圖手機(jī) 檢查注冊表項 百度競價點擊價格的計算公式 硅谷的囚徒呼叫中心 使用U盤裝系統(tǒng) 智能手機(jī)

原生js寫的一個九宮格抽獎程序 (周末在家閑著也是閑著)
我寫的這個抽獎是拿來整蠱我女朋友, 因為每次抽獎的結(jié)果都是同一個。。
哈哈哈哈哈哈哈哈哈哈哈哈哈
我會附上正常抽獎的的代碼和固定只能抽到某一個的代碼;

HTML代碼如下 ⤵️

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>抽獎</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        html,
        body,
        .modal-cover {
            width: 100%;
            height: 100%;
        }

        body {
            background: url('./img/background.jpg') no-repeat center;
            background-size: 100% 100%;
            overflow: hidden;
        }

        .container {
            width: 90%;
            height: 300px;
            /* width: 831px; */
            height: 336px;
            /* border: 1px solid; */
            display: flex;
            flex-wrap: wrap;
            margin: 100px auto;
            border-radius: 6px;
            background: #fff;
            padding: 5px 0 5px 10px;
        }

        .item {
            width: 30%;
            height: 30%;
            /* outline: 1px solid black; */
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 18;
            border-radius: 2px;
            /* border: 1px solid; */
            margin: 4px;
            box-shadow: 1px 1px 14px #ccc;
            position: relative;
        }

        .cover {
            position: absolute;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.3);
            top: 0;
            left: 0;
            border-radius: 2px;
        }

        .item-box {
            width: 100%;
            height: 100%;
            background-size: 100% 100%;
            background-repeat: no-repeat;
        }

        .btn-box {
            background-color: #faa5b6;
            cursor: pointer;
            color: #fff;
            font-size: 14px;
            text-align: center;
            line-height: 100px;
        }

        .modal {
            width: 80%;
            height: 150px;
            position: fixed;
            top: 50%;
            left: 50%;
            background: rgba(255, 255, 255, 255);
            border-radius: 4px;
            transform: translate(-50%, -50%);
            text-align: center;
            padding: 20px 10px 10px;
            z-index: 2;
        }

        .modal .confirm-btn {
            background: pink;
            width: 170px;
            height: 40px;
            color: #fff;
            text-align: center;
            line-height: 40px;
            margin: 0 auto;
            margin-top: 20px;
            cursor: pointer;
            border-radius: 4px;
        }

        .modal-cover {
            position: fixed;
            top: 0;
            left: 0;
            background: rgba(0, 0, 0, 0.3);
            z-index: 1;
            display: none;
        }
    </style>

</head>

<body>
    <div class="container">
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
        <div class="btn-box item">
            <div class="cover" style="background: none;"></div>
            <div class="item-box">
                開始抽獎
            </div>
        </div>
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
        <div class="item">
            <div class="cover"></div>
            <div class="item-box"></div>
        </div>
    </div>
    <div class="modal-cover"></div>
    <div class="modal" style="display: none;">
        <span></span>
        <div class="confirm-btn">不信邪!再試一次!</div>
    </div>
</body>
<script src="./index.js"></script>

</html>

js代碼👇

// 封裝工具函數(shù)
const util = {
    getELe: (str) => {
        return document.querySelector(str)
    },
    getELes: (str) => {
        return document.querySelectorAll(str)
    }
}
let items = util.getELes(".item-box"),
    covers = util.getELes('.cover'),
    imgArr = ['CHANEL', 'Dior', 'givenchy', 'dabao', 'empty', 'lamer', 'lancome', 'yashilandai', 'YSL', ]

for (let i = 0; i < items.length; i++) {
    if (imgArr[i] === 'empty') continue;
    let el = items[i];
    el.style.backgroundImage = `url(./img/${imgArr[i]}.png)`
}
let modal = util.getELe('.modal'),
    mask = util.getELe('.modal-cover'),
    modalInner = util.getELe('.modal span');
let tryBtn = util.getELe('.confirm-btn');
// 存放每一個獎項的下標(biāo)
let arr = [0, 1, 2, 5, 8, 7, 6, 3],
    i = 0,
    count = 0,
    stopTimer;
let rand = Math.floor(Math.random() * 8 + 50);
const rotate = () => {
    // 先給所有的獎項盒子加蒙層
    for (let j = 0; j < arr.length; j++) {
        covers[arr[j]].style.background = 'rgba(0, 0, 0, 0.3)';
    }
    // 將當(dāng)前獎項的遮罩層去除
    covers[arr[i]].style.background = 'none';
    i++;
    if (i === arr.length) {
        i = 0;
    }
     // 通過count調(diào)整旋轉(zhuǎn)速度
    count++;
    // 根據(jù)count 重新調(diào)整計時器速度
    if (count === 5 || count === 45) {
        clearInterval(stopTimer);
        stopTimer = setInterval(rotate, 200);
    }
    if (count === 10 || count === 35) {
        clearInterval(stopTimer);
        stopTimer = setInterval(rotate, 100);
    }
    if (count === 15) {
        clearInterval(stopTimer);
        stopTimer = setInterval(rotate, 50);
    }
    // 固定抽中某個獎項
    // if (count === 40) {
    //     clearInterval(stopTimer);
    //     count = 0;
    //     rand = 0;
    //     setTimeout(() => {
    //         modalInner.innerText = '親!恭喜你中獎大寶SOD蜜一瓶!^_^ 😄';
    //         modal.style.display = 'block'
    //         mask.style.display = 'block'
    //     }, 500);
    // }

    // 當(dāng)?shù)扔谏厦娴碾S機(jī)數(shù)時
    if (count === rand) {
        clearInterval(stopTimer);
    }
    // 點擊再試一次
    tryBtn.addEventListener('click', () => {
        modal.style.display = 'none'
        mask.style.display = 'none'
    })


}
// 給開始按鈕綁定點擊事件 點擊后執(zhí)行 rotate 
const start = () => {
    console.log(count)
    clearInterval(stopTimer);
    stopTimer = setInterval(rotate, 300);
}
covers[4].addEventListener("click", start);

如果想設(shè)置固定抽中某個獎項,// if (count === 40) 這個count的值需要你自己去算一下,圖片自己選幾個。
代碼copy可直接運(yùn)行。
最終效果,有點丑。你們想玩的自己發(fā)揮下吧。

到此這篇關(guān)于html5實現(xiàn)九宮格抽獎可固定抽中某項獎品的文章就介紹到這了,更多相關(guān)html5九宮格抽獎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:通遼 賀州 山南 黃山 湖北 湘潭 懷化 煙臺

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html5實現(xiàn)九宮格抽獎可固定抽中某項獎品》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    蓝田县| 丹凤县| 逊克县| 嵩明县| 聂荣县| 南溪县| 湖口县| 论坛| 颍上县| 壶关县| 西林县| 左云县| 宜宾县| 湾仔区| 调兵山市| 桂林市| 白河县| 乌鲁木齐县| 三亚市| 卫辉市| 沐川县| 东莞市| 蕉岭县| 广丰县| 绍兴市| 南宁市| 商丘市| 栖霞市| 祁东县| 库伦旗| 平利县| 邛崃市| 铜陵市| 叶城县| 泗水县| 高雄市| 新津县| 措勤县| 拜城县| 吉木乃县| 虹口区|