抽奖

作者: zsyyyyy | 来源:发表于2019-05-26 11:59 被阅读0次
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
                list-style: none;
                text-decoration: none;
            }
            .luckyDraw{
                width: 420px;
                margin: 0 auto;
            }
            .prize{
                font-size: 30px;
            }
            .lottery{
                width: 240px;
                margin: 30px auto;
                position: relative;
            }
            .lottery li{
                width: 80px;
                height: 80px;
                border: 1px solid #000;
                box-sizing: border-box;
                text-align: center;
                line-height: 80px;
                position: absolute;
            }
            .lottery li:nth-of-type(1){
                left: 0;
                top: 0;
            }
            .lottery li:nth-of-type(2){
                left: 80px;
                top: 0;
            }
            .lottery li:nth-of-type(3){
                left: 160px;
                top: 0;
            }
            .lottery li:nth-of-type(4){
                left: 160px;
                top: 80px;
            }
            .lottery li:nth-of-type(5){
                left: 160px;
                top: 160px;
            }
            .lottery li:nth-of-type(6){
                left: 80px;
                top: 160px;
            }
            .lottery li:nth-of-type(7){
                left: 0;
                top: 160px;
            }
            .lottery li:nth-of-type(8){
                left: 0;
                top: 80px;
            }
            .lottery li:nth-of-type(9){
                left: 80px;
                top: 80px;
                cursor: pointer;
                background: gold;
            }
            .active:after{
                position: absolute;
                top: 0;
                left: 0;
                content: "";
                width: 100%;
                height: 100%;
                background: rgba(0,0,0,0.1);
            }
        </style>
    </head>
    <body>
        <div class="luckyDraw">
            <ul class="lottery">
                <li class="active">1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>6</li>
                <li>7</li>
                <li>8</li>
                <li>点击抽奖</li>
            </ul>
            <span class="prize">奖品</span>
        </div>
        <script type="text/javascript">
            var arr = [1,2,3,4,5,6,7,8];//奖品
            var lottery = document.querySelector(".lottery");
            var prize = document.querySelector(".prize");
            var ali = lottery.querySelectorAll("li");
            var i=0;        //当前转到哪个位置
            var count = 0;  //转圈数初始值
            var totalCount = 9; //转动的总圈数
            var index = 1;  //默认中奖位置
            var speed = 500;//旋转速度初始值
            var minSpeed = 500;//旋转最小速度
            var timer; //定时器 
            var isClick = true;
            //前两次只能中2
            
            function roll(){
                speed -=50;
                if(speed<=10){speed=10}
                var child = ali[i].parentNode.children; 
                for(var j=0;j<child.length;j++){
                    child[j].classList.remove("active");
                }
                i++;
//              console.log(i);
                if(i>=ali.length-1){//8
                    i=0;
                    count++;            
                }
                prize.innerHTML = arr[i];
                ali[i].classList.toggle("active");
//              当转到第九圈的时候就停止,随机数index是[1,8],i是[0,8],随意随机数需要index-1,清除计时器
                if(count>=totalCount&&i==index-1){  //if(count==totalCount&&i==index)
                    clearTimeout(timer);
                    isClick=true;
                    speed=minSpeed;
//                  console.log(count);
                }else{//这个else是啥意思
                    //转的圈数等于8和9的时候就是最后两圈,或者speed<=50开始减速
                    if(count>=totalCount-1||speed<=50){
                        speed+=100//减速
                    }
                    //计时器不能放到判断外面,  
                    timer = setTimeout(roll,speed);//调用自己
                }
            }
            console.log(ali.length)//9
            var onclicktime =0;
            ali[ali.length-1].onclick = function(){
                //前两次只能中2
                if(onclicktime<2&&isClick){//前两次只能中2
                  count=0;
                    index = 2;// 中奖位置
                    console.log("中奖位置:"+index);
                    isClick=false;
                    roll();
                    onclicktime+=1;
                    console.log(onclicktime)
                }
                else if(onclicktime>=2&&isClick){
                    count=0;
                    index = Math.floor(Math.random()*arr.length+1);//中奖位置
//                  Math.floor((Math.random()*8)+1);//[1,8]
                    //随机产生中奖位置
//                  index = 1;设置中第几个
                    console.log("中奖位置:"+index);
                    isClick=false;
                    roll();
                }
            }       
        </script>
    </body>
</html>

相关文章

  • 文末||抽奖,抽奖,抽奖

    关注公众号“14摄氏度” 转发朋友圈截图发送给后台获取抽奖密令 人工审核请耐心等待 10月8日开奖 因为14摄氏度...

  • 抽奖》抽奖

    #作业要求: 1.登录成功后,用户选择幸运抽奖菜单,进入幸运抽奖功能 2.输入会员卡号,系统生成5个4位随机数作为...

  • 抽奖抽奖

    “我是小虹”第三届简书贝抽奖活动今天落幕了。 感谢以下简友参与和评论: 评论回复主要喜欢吃的美食,有: 饺子、草莓...

  • 从抽奖助手谈微信抽奖小程序!

    说抽奖小程序之前,先整体了解下抽奖核心。 抽奖心理:抽奖在想什么?抽奖假象:越多越多?抽奖玩法:抽奖本质,能不能解...

  • Express + Three.js 抽奖程序

    抽奖程序 年会抽奖程序,3D 球体抽奖,支持奖品信息配置,参与抽奖人员信息Excel导入,抽奖结果Excel导出 ...

  • 顺风肥牛搞事情了

    吃火锅抽奖 吃酸菜鱼抽奖 吃烤鱼抽奖 吃烧烤抽奖 吃啥都抽奖 咔咔就是抽,咔咔就是送

  • 定时抽奖活动怎么做?

    玩腻了签到抽奖,也玩过了拼团抽奖,还有什么抽奖玩法是没有玩过的?不如试试定时抽奖!!!预定好时间抽奖,开奖之前的忐...

  • Scratch—转盘抽奖

    【知识延伸】 传统抽奖分为抽奖盒抽取、转盘抽奖等 【要求】 今天我们按照平时转盘抽奖、利用Scratch做一个转盘...

  • 抽奖js源码收集整理

    js大富翁抽奖游戏源码 jQueryRotate圆盘抽奖插件 其他27款js抽奖插件下载

  • 抽奖啦抽奖啦!!

    抽奖啦抽奖啦!!!微信扫哦!

网友评论

      本文标题:抽奖

      本文链接:https://www.haomeiwen.com/subject/fouxtctx.html