• js实现抽奖


    抽奖.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <script type="text/javascript" src="index.js"></script>
    </head>
    <body>
    <div id="title" class="title">开始抽奖啦</div>
    <div class="btns">
    <span id="play">开始</span>
    <span id="stop">结束</span>
    </div>
    </body>
    </html>
     
    index.js代码如下:
    var data=['iPhone5','IPad','三星笔记本','佳能相机','惠普打印机'],
    timer = null,
    flag=0;

    window.onload = function(){
    var play = document.getElementById('play'),
    stop = document.getElementById('stop');

    //开始抽奖
    play.onclick=playFun;
    stop.onclick = stopFun;

    //键盘事件
    document.onkeyup = function(event){
    event = event || window.event;
    console.log(event.keyCode);
    if(event.keyCode==13)
    {
    if(flag==0){
    playFun();
    flag=1;
    }
    else {
    stopFun();
    flag = 0;
    }
    }
    }
    }

    function playFun(){
    //var that = this;
    var title = document.getElementById('title');
    var play = document.getElementById('play');
    clearInterval(timer);
    timer = setInterval(function(){
    var random = Math.floor(Math.random()*data.length);
    console.log(random);
    title.innerHTML = data[random];
    },50);
    play.style.background ='#999';
    }

    function stopFun(){
    clearInterval(timer);
    var play = document.getElementById('play');
    play.style.background ='#036';
    }

    index.css代码如下:
    *
    {
    margin:0;
    padding:0;
    }

    .title {
    400px;
    height:70px;
    margin:0 auto;
    padding-top:30px;
    text-align:center;
    font-size:24px;
    font-weight:bold;
    color:#F00;
    }

    .btns {
    190px;
    height:30px;
    margin:0 auto;
    }

    .btns span {
    display:block;
    float:left;
    80px;
    height:27px;
    line-height: 27px;
    background:#036;
    border:1px solid #eee;
    border-radius:7px;
    margin-right:10px;
    color:#FFF;
    text-align:center;
    font-size:14px;
    font-family:"微软雅黑";
    cursor:pointer;
    }
     
     
     
  • 相关阅读:
    读取纯真IP数据库C++源代码
    Ubuntu 安装VMware tools 解决方法讨论
    VMware虚拟机磁盘压缩
    使用 TestLink 进行测试管理
    正则表达式30分钟入门教程(转)
    HDU 1325 Is It A Tree? POJ 1308 Is It A Tree? (并查集+入度,判断一个有向图是树)
    POJ 1703 Find them, Catch them (并查集)
    POJ 1182 食物链(并查集)
    POJ 1984 Navigation Nightmare (并查集)
    POJ 2236 Wireless Network (并查集)
  • 原文地址:https://www.cnblogs.com/klsw/p/8641988.html
Copyright © 2020-2023  润新知