weixin_54077820 2021-06-14 11:08 采纳率: 50%
浏览 35
已采纳

怎样才能抽不中平板和电脑

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #prize{
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            top: 100px;
            display: block;
            width: 320px;
            height: 40px;
            background-color: rgb(255, 255, 255);
            border-radius: 2px;
            box-shadow: rgba(0, 0, 0, 0.3) 0 0 3px 2px;
            letter-spacing: 2px;
            text-align: center;
            line-height: 40px;
        }
        div{
            width: 330px;
            height: 330px;
            position: relative;
            left: 50%;
            top:50%;
            transform:translate(-50%,50%);
        }
        div>span{
            float: left;
            width: 100px;
            height: 100px;
            margin: 5px;
            background: rgb(22, 186, 236);
            color: white;
            text-align: center;
            line-height: 100px;
            
        }
        div>span:nth-of-type(4){
            position: relative;
            left: 220px;
        }
        div>span:nth-of-type(5){
            position: relative;
            left: 110px;
            top:110px;
        }
        div>span:nth-of-type(6){
            position: relative;
            left: -110px;
            top:110px;
        }
        div>span:nth-of-type(8){
            position: relative;
            left: -110px;
            top:-110px;
        }
        div>span:nth-of-type(9){
            cursor: pointer;
            background: rgb(255, 148, 61);
            position: relative;
            left: -110px;
            top:-110px;
        }
        div .aa{
            background: rgb(78, 78, 78);
        }
    </style>
</head>
<body>
    <p id='prize'></p>
    <div>
        <span class="aa">手机</span>
        <span>平板</span>
        <span>电磁炉</span>
        <span>洗衣机</span>
        <span>衣柜</span>
        <span>冰箱</span>
        <span>电脑</span>
        <span>电视</span>
        <span>开始</span>
    </div>
</body>
<script>
    var prize= document.getElementById('prize');
    var spans = document.querySelectorAll("div>span");
        function start(){
            // if(typeof(num)=='undefined'){
            //     // spans[num].classList.remove('aa'); // 清空上一次结果
            //     console.log('hhh')
            // }
            spans.forEach(function(el,index){
                if(index!=0){
                    el.classList.remove('aa'); // 清空上一次结果
                }
            })
            prize.textContent='';   
            spans[8].style.cursor="not-allowed";
            spans[8].onclick=null;
            let num=-1;      //奖品序号
            let times=parseInt(Math.random()*(30-18+1)+18,10);
            let time=0;     //当前的旋转次数
            let speed=100;  //转盘速度
                timer = setInterval(function(){
                num++;
                time++;
                if(num > 7){
                    num = 0;
                    spans[0].classList.add('aa');
                    spans[7].classList.remove('aa');
                }else if(num==0){
                    spans[num].classList.add('aa');
                    spans[7].classList.remove('aa');
                }else{
                    spans[num].classList.add('aa');
                    spans[num-1].classList.remove('aa');
                } 
                if(time>times){
                    spans[8].onclick=start;
                    spans[8].style.cursor="pointer";
                    clearInterval(timer);
                    prize.textContent='恭喜您抽中了'+spans[num].textContent+'!!!';
                }
            },speed)
        }
    spans[8].onclick=start;
</script>
</html>
  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2021-06-14 14:13
    关注

    通过times可以计算得到的奖项,如果是平板(1)或者电脑(6)times+1将奖项设置为下一个

    帮助到你可以点击采纳吗,谢谢~~

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
    
            #prize {
                position: absolute;
                left: 50%;
                transform: translateX(-50%);
                top: 100px;
                display: block;
                width: 320px;
                height: 40px;
                background-color: rgb(255, 255, 255);
                border-radius: 2px;
                box-shadow: rgba(0, 0, 0, 0.3) 0 0 3px 2px;
                letter-spacing: 2px;
                text-align: center;
                line-height: 40px;
            }
    
            div {
                width: 330px;
                height: 330px;
                position: relative;
                left: 50%;
                top: 50%;
                transform: translate(-50%,50%);
            }
    
            div > span {
                float: left;
                width: 100px;
                height: 100px;
                margin: 5px;
                background: rgb(22, 186, 236);
                color: white;
                text-align: center;
                line-height: 100px;
            }
    
            div > span:nth-of-type(4) {
                position: relative;
                left: 220px;
            }
    
            div > span:nth-of-type(5) {
                position: relative;
                left: 110px;
                top: 110px;
            }
    
            div > span:nth-of-type(6) {
                position: relative;
                left: -110px;
                top: 110px;
            }
    
            div > span:nth-of-type(8) {
                position: relative;
                left: -110px;
                top: -110px;
            }
    
            div > span:nth-of-type(9) {
                cursor: pointer;
                background: rgb(255, 148, 61);
                position: relative;
                left: -110px;
                top: -110px;
            }
    
            div .aa {
                background: rgb(78, 78, 78);
            }
        </style>
    </head>
    <body>
        <p id='prize'></p>
        <div>
            <span class="aa">手机</span>
            <span>平板</span>
            <span>电磁炉</span>
            <span>洗衣机</span>
            <span>衣柜</span>
            <span>冰箱</span>
            <span>电脑</span>
            <span>电视</span>
            <span>开始</span>
        </div>
    </body>
    <script>
        var prize = document.getElementById('prize');
        var spans = document.querySelectorAll("div>span");
        function start() {
            // if(typeof(num)=='undefined'){
            //     // spans[num].classList.remove('aa'); // 清空上一次结果
            //     console.log('hhh')
            // }
            spans.forEach(function (el, index) {
                if (index != 0) {
                    el.classList.remove('aa'); // 清空上一次结果
                }
            })
            prize.textContent = '';
            spans[8].style.cursor = "not-allowed";
            spans[8].onclick = null;
            let num = -1;      //奖品序号
            let times = parseInt(Math.random() * (30 - 18 + 1) + 18, 10); 
            let hits = [1, 6],//平板和电脑的下标,从0开始
                prizeIndex = times % 8;//得到奖项
            if (hits.findIndex(i => i == prizeIndex) != -1) {
                times += 1;//如果得奖的是hits中奖项,不将奖项设置为下一项
            }
      
            let time = 0;     //当前的旋转次数
            let speed = 100;  //转盘速度
            timer = setInterval(function () {//1,6
                num++;
                time++;
                if (num > 7) {
                    num = 0;
                    spans[0].classList.add('aa');
                    spans[7].classList.remove('aa');
                } else if (num == 0) {
                    spans[num].classList.add('aa');
                    spans[7].classList.remove('aa');
                } else {
                    spans[num].classList.add('aa');
                    spans[num - 1].classList.remove('aa');
                }
                if (time > times) {
                    spans[8].onclick = start;
                    spans[8].style.cursor = "pointer";
                    clearInterval(timer);
                    prize.textContent = '恭喜您抽中了' + spans[num].textContent + '!!!';
                }
            }, speed)
        }
        spans[8].onclick = start;
    </script>
    </html>
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)