html5实现九宫格抽奖可固定抽中某项奖品


Posted in HTML / CSS onJune 15, 2020

原生js写的一个九宫格抽奖程序 (周末在家闲着也是闲着)
我写的这个抽奖是拿来整蛊我女朋友, 因为每次抽奖的结果都是同一个。。
哈哈哈哈哈哈哈哈哈哈哈哈哈
我会附上正常抽奖的的代码和固定只能抽到某一个的代码;

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代码?

// 封装工具函数
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');
// 存放每一个奖项的下标
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)';
    }
    // 将当前奖项的遮罩层去除
    covers[arr[i]].style.background = 'none';
    i++;
    if (i === arr.length) {
        i = 0;
    }
     // 通过count调整旋转速度
    count++;
    // 根据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);
    }
    // 固定抽中某个奖项
    // if (count === 40) {
    //     clearInterval(stopTimer);
    //     count = 0;
    //     rand = 0;
    //     setTimeout(() => {
    //         modalInner.innerText = '亲!恭喜你中奖大宝SOD蜜一瓶!^_^ ?';
    //         modal.style.display = 'block'
    //         mask.style.display = 'block'
    //     }, 500);
    // }

    // 当等于上面的随机数时
    if (count === rand) {
        clearInterval(stopTimer);
    }
    // 点击再试一次
    tryBtn.addEventListener('click', () => {
        modal.style.display = 'none'
        mask.style.display = 'none'
    })


}
// 给开始按钮绑定点击事件 点击后执行 rotate 
const start = () => {
    console.log(count)
    clearInterval(stopTimer);
    stopTimer = setInterval(rotate, 300);
}
covers[4].addEventListener("click", start);

如果想设置固定抽中某个奖项,// if (count === 40) 这个count的值需要你自己去算一下,图片自己选几个。
代码copy可直接运行。
最终效果,有点丑。你们想玩的自己发挥下吧。

html5实现九宫格抽奖可固定抽中某项奖品

到此这篇关于html5实现九宫格抽奖可固定抽中某项奖品的文章就介绍到这了,更多相关html5九宫格抽奖内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
超酷炫 CSS3垂直手风琴菜单
Jun 28 HTML / CSS
css3实现椭圆轨迹旋转的示例代码
Oct 29 HTML / CSS
浅谈HTML5 defer和async的区别
Jun 07 HTML / CSS
HTML5 FormData 方法介绍以及实现文件上传示例
Sep 12 HTML / CSS
HTML5 WebGL 实现民航客机飞行监控系统
Jul 25 HTML / CSS
html5使用canvas压缩图片的示例代码
Sep 11 HTML / CSS
深入了解canvas在移动端绘制模糊的问题解决
Apr 30 HTML / CSS
HTML5去掉输入框type为number时的上下箭头的实现方法
Jan 03 HTML / CSS
html+css合并表格边框的示例代码
Mar 31 HTML / CSS
CSS 新特性 contain控制页面的重绘与重排问题
Apr 30 HTML / CSS
纯 CSS 自定义多行省略的问题(从原理到实现)
Nov 11 HTML / CSS
新的CSS 伪类函数 :is() 和 :where()示例详解
Aug 05 HTML / CSS
HTML5页面无缝闪开的问题及解决方案
Jun 11 #HTML / CSS
Html5导航栏吸顶方案原理与对比实现
Jun 10 #HTML / CSS
浅析图片上传及canvas压缩的流程
Jun 10 #HTML / CSS
原生canvas制作画图小工具的踩坑和爬坑
Jun 09 #HTML / CSS
h5移动端调用支付宝、微信支付的实现
Jun 08 #HTML / CSS
使用SVG实现提示框功能的示例代码
Jun 05 #HTML / CSS
Html5嵌入钉钉的实现示例
Jun 04 #HTML / CSS
You might like
我的论坛源代码(七)
2006/10/09 PHP
与文件上传有关的php配置参数总结
2013/06/14 PHP
php解析json数据实例
2014/08/19 PHP
php常用的安全过滤函数集锦
2014/10/09 PHP
smarty自定义函数htmlcheckboxes用法实例
2015/01/22 PHP
Yii全局函数用法示例
2017/01/22 PHP
PHP连接MYSQL数据库的3种常用方法
2017/02/27 PHP
PHP中localeconv()函数的用法
2019/03/26 PHP
jquery remove方法应用详解
2012/11/22 Javascript
如何用js控制frame的隐藏或显示的解决办法
2013/03/20 Javascript
js函数名与form表单元素同名冲突的问题
2014/03/07 Javascript
jQuery.Callbacks()回调函数队列用法详解
2016/06/14 Javascript
JavaScript Promise 用法
2016/06/14 Javascript
关于JSON与JSONP简单总结
2016/08/16 Javascript
移动端使用localStorage缓存Js和css文的方法(web开发)
2016/09/20 Javascript
从0开始学Vue
2016/10/27 Javascript
JavaScript静态作用域和动态作用域实例详解
2019/06/17 Javascript
基于JS实现快速读取TXT文件
2020/08/25 Javascript
[05:11]TI9战队采访——VIRTUSPRO
2019/08/22 DOTA
Python教程之全局变量用法
2016/06/27 Python
pandas数据分组和聚合操作方法
2018/04/11 Python
对Python中plt的画图函数详解
2018/11/07 Python
PyQt5实现五子棋游戏(人机对弈)
2020/03/24 Python
Pandas的read_csv函数参数分析详解
2019/07/02 Python
Django中使用CORS实现跨域请求过程解析
2019/08/05 Python
在 Linux/Mac 下为Python函数添加超时时间的方法
2020/02/20 Python
在pycharm中创建django项目的示例代码
2020/05/28 Python
python3读取autocad图形文件.py实例
2020/06/05 Python
python进度条显示-tqmd模块的实现示例
2020/08/23 Python
pandas 数据类型转换的实现
2020/12/29 Python
HTML5实现经典坦克大战坦克乱走还能发出一个子弹
2013/09/02 HTML / CSS
TUMI澳大利亚网站:美国旅行箱包品牌
2017/03/27 全球购物
中专生自荐信
2014/06/25 职场文书
党的群众路线教育实践活动批评与自我批评发言稿
2014/10/16 职场文书
社区母亲节活动总结
2015/02/10 职场文书
活动总结模板大全
2015/05/11 职场文书