Lucky draw: you can draw a certain prize

Time:2020-9-2

A lucky draw program written by original JS
I wrote this lottery is to trick my girlfriend, because the result of each lottery is the same..
Ha ha ha ha ha ha ha ha ha ha ha ha ha ha
I will attach the normal lottery code and fixed can only draw a certain code;

The HTML code is as follows ⤵️

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    < title > raffle
    <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">
                Start the draw
            </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" > don't believe in evil! do it again! </div>
    </div>
</body>
<script></script>

</html>

JS code

//Encapsulating tool functions
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');
//Save the subscript of each award
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 = () => {
    //Cover all the prize boxes first
    for (let j = 0; j < arr.length; j++) {
        covers[arr[j]].style.background = 'rgba(0, 0, 0, 0.3)';
    }
    //Remove the mask layer of the current award
    covers[arr[i]].style.background = 'none';
    i++;
    if (i === arr.length) {
        i = 0;
    }
     //Adjust the rotation speed by count
    count++;
    //Adjust the timer speed according to count
    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);
    }
    //Fixed draw a certain award
    // if (count === 40) {
    //     clearInterval(stopTimer);
    //     count = 0;
    //     rand = 0;
    //     setTimeout(() => {
    //          modalInner.innerText  ='kiss! Congratulations on winning a bottle of SOD honey! Cubic meter ^ ';
    //         modal.style.display = 'block'
    //         mask.style.display = 'block'
    //     }, 500);
    // }

    //When it's equal to the random number above
    if (count === rand) {
        clearInterval(stopTimer);
    }
    //Click to try again
    tryBtn.addEventListener('click', () => {
        modal.style.display = 'none'
        mask.style.display = 'none'
    })


}
//Bind the start button to the click event, and then execute rotate 
const start = () => {
    console.log(count)
    clearInterval(stopTimer);
    stopTimer = setInterval(rotate, 300);
}
covers[4].addEventListener("click", start);

If you want to set a certain prize in a fixed draw, / / if (count = = = 40), you need to calculate the count value yourself, and select a few pictures by yourself.
Code copy can be run directly.
The end result, a little ugly. You want to play your own play.

Lucky draw: you can draw a certain prize

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]