抽奖小游戏

html:

<div id="warp">
            
        </div>
        <button id="start">开始</button>
        <button id="end">停止</button>

css:

 #warp{
                width: 600px;
                height: 600px;
                position: relative;
            }
            #warp div{
                width: 198px;
                height: 198px;
                background-color: #dedede;
                position: absolute;
                border: 1px solid sandybrown;
                line-height: 200px;
                text-align: center;
            }
            #warp .active{
                background-color: red;
                color: white;
            }
            #start{
                width: 80px;
                height: 30px;
                position: relative;
                left: 220px;
                top: -300px;
            }
            #end{
                width: 80px;
                height: 30px;
                position: relative;
                left: 220px;
                top: -300px;
            }

js:

var goodList=["iphone14","苹果电脑","华为p30","兰博基尼","路虎","钻石","10元代金卷","谢谢惠顾"]
           var list=["谢谢惠顾","苹果电脑","iphone14","谢谢惠顾","谢谢惠顾","谢谢惠顾","谢谢惠顾","谢谢惠顾","10元代金卷","10元代金卷","10元代金卷","10元代金卷","10元代金卷","兰博基尼","华为p30"]
           var randomGood  //随机的商品
           //获取a,b之间任意一个的随机数
           function random(a,b){
               return Math.floor(Math.random()*(b-a+1))+a
           }
           // console.log(random(0,list.length-1))
           for(var i=0;i<goodList.length;i++){
               warp.innerHTML+=`<div>${goodList[i]}</div>`
           } 
           var items=warp.children//获取warp子元素
           items[1].style.left="200px"
           items[2].style.left="400px"
           items[3].style.left="400px"
           items[3].style.top="200px"
           items[4].style.left="400px"
           items[4].style.top="400px"
           items[5].style.left="200px"
           items[5].style.top="400px" 
           items[6].style.top="400px"
           items[7].style.top="200px"
           
           var x=0//记录当前要高亮的div
           items[x].className="active"
           
           var t  //定时器的初始值
           
           function f1(){
               items[x].className=""//把高亮状态取消
               x++
               if(x>7){
                   x=0
               }
               items[x].className="active"//把高亮状态加上
               if(flag){
                   t-=10
                   if(t<=80){
                       t=80
                   }
               }else{
                    t+=10
                    if(t>=500){
                        t=500
                        if(items[x].innerHTML==randomGood){
                            // alert("恭喜你抽取了")
                            flag=true
                            return
                        }
                    }
                }
               setTimeout(f1,t)
           }
           var flag=true
           //点击开始按钮
           start.οnclick=function(){
            if(flag=true){
                t=200
                randomGood=list[random(0,list.length-1)]//中间商品
                f1()
            }   
           }
           end.οnclick=function(){
               flag=false
           }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值