原生js实现抽奖效果

本人为初学者,写的不好的请不要见怪

目的是用节简的代码来实现抽奖效果

下图为基本的布局


通过点击GO按钮可以实现滚动,可连续点击。

以下为代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #dalor{
            width: 400px;
            height: 400px;
            position: absolute;
            margin-top: -200px;
            margin-left: -200px;
            top: 50%;
            left: 50%;
        }
        #dalor div{
            position: absolute;
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;
        }
        #dalor_go{
            position: absolute;
            width: 200px;
            height: 200px;
            top: 100px;
            left: 100px;
            line-height: 200px;
            background: red;
            color: white;
            text-align: center;
            border: none;
            cursor: pointer;
        }
        .dalor1{
            top: 0;
            left: 0;
            background: #FF9999;
        }
        .dalor2{
            top: 0;
            left: 100px;
            background: #CDCD66;
        }
        .dalor3{
            top: 0;
            left: 200px;
            background: #FF9999;
        }
        .dalor4{
            top: 0;
            left: 300px;
            background: #CDCD66;
        }
        .dalor5{
            top: 100px;
            left: 300px;
            background: #FF9999;
        }
        .dalor6{
            top: 200px;
            left: 300px;
            background: #CDCD66;
        }
        .dalor7{
            top: 300px;
            left: 300px;
            background: #FF9999;
        }
        .dalor8{
            top: 300px;
            left: 200px;
            background: #CDCD66;
        }
        .dalor9{
            top: 300px;
            left: 100px;
            background: #FF9999;
        }
        .dalor10{
            top: 300px;
            left: 0;
            background: #CDCD66;
        }
        .dalor11{
            top: 200px;
            left: 0;
            background: #FF9999;
        }
        .dalor12{
            top: 100px;
            left: 0;
            background: #CDCD66;
        }
        .blue{
            background: #40FFE5;
        }
    </style>
</head>
<body>
<div id="dalor">
    <input id="dalor_go" type="button" value="go!">
    <div class="dalor1 blue">1</div>
    <div class="dalor2">2</div>
    <div class="dalor3">3</div>
    <div class="dalor4">4</div>
    <div class="dalor5">5</div>
    <div class="dalor6">6</div>
    <div class="dalor7">7</div>s
    <div class="dalor8">8</div>
    <div class="dalor9">9</div>
    <div class="dalor10">10</div>
    <div class="dalor11">11</div>
    <div class="dalor12">12</div>
</div>
<script>
    window.οnlοad=function () {
        var odar=document.getElementById("dalor").getElementsByTagName("div");
        var odarGo=document.getElementById("dalor_go");
        var suzi=0;
        odarGo.addEventListener("click",function () {
            var odartime=Math.ceil(Math.random()*24+24);
            var xunuantimu=setInterval(function () {
                odar[suzi%12].classList.remove("blue");
                odar[(++suzi)%12].classList.add("blue");
                if (suzi%odartime==0){
                   clearInterval(xunuantimu);
                }
            },100);
        });
    }

</script>
</body>
</html>
主要通过suzi来判断整体的运作,通过求余数来实现停止,增加来实现滚动的效果。



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值