温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JS如何实现简单九宫格抽奖

发布时间:2022-07-02 14:01:20 阅读:203 作者:iii 栏目:开发技术
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍了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{
    width450px;
    height450px;
    background-color#9a6e3a;
    border2px solid #990055;
    position: relative;
    z-index10;
}
.box>ul>li{
    position: absolute;
    width148px;
    height148px;
    border1px #222222 solid;
    background-color#ad889d;
}
.box>ul>li>a{
    display: block;
    color#fff;
    font-size30px;
    text-align: center;
    line-height148px;
}
/* 开始按钮 */
.btn{
    position: absolute;
    top150px;
    left150px;
    width150px;
    height150px;
    border1px #222222 solid;
    background-color#7d53c7;
    outline: none;
    cursor: pointer;
    color#fff;
    font-size25px;
    transition: all 0.5s;
    z-index20;
}
.btn:hover{
    background-color#df04ff;
    font-size30px;
}
.btn:active{
    background-color#7d53c7;
}
.box>ul>li.on{
    background-color#f00;
}
/* 开始按钮 end */

/* 奖品定位 */
.box>ul>li:nth-child(1){
    top0;
    left0;
}
.box>ul>li:nth-child(2){
    top0;
    left150px;
}
.box>ul>li:nth-child(3){
    top0;
    left300px;
}
.box>ul>li:nth-child(4){
    top150px;
    left300px;
}
.box>ul>li:nth-child(5){
    top300px;
    left300px;
}
.box>ul>li:nth-child(6){
    top300px;
    left150px;
}
.box>ul>li:nth-child(7){
    top300px;
    left0;
}
.box>ul>li:nth-child(8){
    top150px;
    left0;
}
/* 奖品定位 end */

/* 提示框 */
.tishi{
    width300px;
    height146px;
    border2px #990055 solid;
    background#92EC1F;
    border-radius20px;
    position: absolute;
    left50%;
    top50%;
    margin-top: -75px;
    margin-left: -150px;
    z-index30;
    text-align: center;
    font-size25px;
    font-weight: bold;
    display: none;
    animation: tishiAni 0.5s both;
    transition: all 0.5s;
}
.tishi>p{
    color: red;
    font-size28px;
    margin-top20px;
}
.tishi>button{
    width60px;
    height30px;
    border:none;
    outline: none;
    cursor: pointer;
    position: absolute;
    right30px;
    bottom20px;
}
@keyframes tishiAni {
    0%{
        opacity0;
    }
    100%{
        opacity1;
    }
}
/* 提示框 end */

通过计时器来实现奖品亮块的循环

JavaScript文件:

{
    let jiangPinChi = [
        "奖品1",
        "奖品2",
        "奖品3",
        "奖品4",
        "奖品5",
        "奖品6",
        "奖品7",
        "奖品8",
    ];

    let index = 0;
    let times = Math.roundMath.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] );
        forlet 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.roundMath.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如何实现简单九宫格抽奖”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI

开发者交流群×