JS实现简单九宫格抽奖

 更新时间:2022年06月28日 15:42:26   作者:ResidualBridge  
这篇文章主要为大家详细介绍了JS实现简单九宫格抽奖,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用JavaScript写一个九宫格的抽奖盘,供大家参考,具体内容如下

点击中间的块,选中奖品的亮块会在边缘的8个块循环;

选中后,弹出选中的内容;

代码参考:

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";
    });
}

当然,这个可以扩展成更多的类型,实现不止九宫格的奖盘抽奖模式。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • checkbox选中与未选中判断示例

    checkbox选中与未选中判断示例

    本节主要介绍了checkbox选中与未选中的判断方法,需要的朋友可以参考下
    2014-08-08
  • js前端加载超大图片(100M以上)实现秒开的最佳解决方案

    js前端加载超大图片(100M以上)实现秒开的最佳解决方案

    前端加载超大图片时,一般可以采取图片压缩,图片分割,预加载等措施,而对于几百M或上G的大图而言,不管对图片进行怎么优化或加速处理,要实现秒开也是不太可能的事情,所以本文小编将给大家介绍如何对大图进行分割,在前端进行拼接实现秒开,需要的朋友可以参考下
    2023-10-10
  • 微信小程序获取地理位置及经纬度授权代码实例

    微信小程序获取地理位置及经纬度授权代码实例

    这篇文章主要介绍了微信小程序获取地理位置及经纬度授权代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • JavaScript鼠标拖拽事件详解

    JavaScript鼠标拖拽事件详解

    这篇文章主要为大家详细介绍了JavaScript鼠标拖拽事件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • 基于JS实现带动画效果的流程进度条

    基于JS实现带动画效果的流程进度条

    当在使用流程的时候,比如有一个审核流程,有三个阶段:开始,审核中,审核成功。当在不同的阶段,做相应的进度显示,当显示时,是以动画的形式显示的。下面通过代码给大家介绍JS实现带动画效果的流程进度条,感兴趣的朋友一起看看吧
    2018-06-06
  • js实现图片轮播效果

    js实现图片轮播效果

    这篇文章主要介绍了js实现图片轮播效果的相关资料,需要的朋友可以参考下
    2015-12-12
  • 前端点击预览图片Viewer.js使用方法(简单的操作)

    前端点击预览图片Viewer.js使用方法(简单的操作)

    这篇文章主要介绍了前端点击预览图片Viewer.js使用的相关资料,Viewer.js是一款强大的图片查看器,支持移动设备触摸事件、响应式、放大/缩小、旋转、翻转、移动、键盘操作、全屏幻灯片模式、缩略图和标题显示等功能,需要的朋友可以参考下
    2025-01-01
  • js点击按钮实现水波纹效果代码(CSS3和Canves)

    js点击按钮实现水波纹效果代码(CSS3和Canves)

    这篇文章主要为大家详细介绍了点击按钮实现水波纹效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • BootStrap表单验证实例代码

    BootStrap表单验证实例代码

    这篇文章主要介绍了bootstrap表单验证的实例代码,代码中包括引入的js文件,具体实现方法,大家参考本文
    2017-01-01
  • Javascript中JSON数据分组优化实践及JS操作JSON总结

    Javascript中JSON数据分组优化实践及JS操作JSON总结

    这篇文章主要介绍了Javascript中JSON数据分组优化实践,文中还对JS操作JSON的要领做了总结,需要的朋友可以参考下
    2017-12-12

最新评论