首页 >web前端 >js教程 >js实现抽奖系统功能代码分享

js实现抽奖系统功能代码分享

小云云
小云云原创
2018-02-08 13:15:262807浏览

本文主要和大家详细介绍了一个关于抽奖系统的题目之js抽奖系统功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助大家运用js开发一个抽系统。

要求实现功能:

1.点击左侧按键,开始抽奖,点击右侧按键,停止抽奖;

2.敲击回车键,开始抽奖,再次敲击回车键,停止抽奖;

3.开始抽奖后,左侧按钮变色;

html:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>抽奖</title>
  <link rel="stylesheet" href="style.css">
  <script src="eventUtil.js"></script>
  <script src="js.js"></script>
</head>
<body>
<p class="box">
  <p id="header">开始抽奖啦!</p>
  <p id="btn">
    <span id="start">开始</span>
    <span id="stop">结束</span>
  </p>
</p>
</body>
</html>

css:


body{
  margin: 0;
  padding: 0;
}
.box{
  width: 400px;
  height: 200px;
  border: 1px solid #0C4E7C;
  margin: 0 auto;
}
#header{
  color:darkred;
  font-size: 24px;
  text-align: center;
  width: 400px;
  height: 60px;
  line-height: 60px;
}
#btn{
  width: 200px;
  overflow: hidden;
  margin: 30px auto 0;
}
#btn span{
  cursor: pointer;
  border: 2px solid #a09a09;
  border-radius: 7px;
  margin-right: 10px;
  color: #000;
  display: inline-block;
  height: 40px;
  width: 80px;
  background-color: #f2ec55;
  line-height: 40px;
  text-align: center;
}

JavaScript:


js.js:
var date = ["谢谢参与", "谢谢参与", "谢谢参与", "50元话费", "ipad", "佳能相机", "苹果手机", "3DS", "switch", "1000元超市购物卡"];
var timer = null;
var flag = 0;


window.onload = function () {

  // var header = document.getElementById("header");优化前
  var start = document.getElementById("start");
  var stop = document.getElementById("stop");
//鼠标抽奖
  eventUtil.addHandler(start, "click", getStart);
  eventUtil.addHandler(stop, "click", getStop);
//键盘抽奖;
  document.onkeyup = function (event) {
    var e = event || window.event;
    //检测按键键值;
    // console.log(e.keyCode);
    if (e.keyCode === 13) {
      if (flag === 0) {
        getStart();
        flag = 1;
      } else {
        getStop();
        flag = 0;
      }
    }
  };

  function getStart() {
    clearInterval(timer);
    var header = document.getElementById("header");//优化后
    timer = setInterval(function () {
      //代码优化前
      //var x = parseInt(Math.random()*10);
      var random = Math.floor(Math.random() * date.length);
      header.innerHTML = date[random];
    }, 50);
    start.style.backgroundColor = "#999";
    flag = 1;
  }

  function getStop() {
    clearInterval(timer);
    start.style.backgroundColor = "#f2ec55";
    flag = 0;
  }
};

eventUtil.js


var eventUtil = {
  getEvent:function (event) {
    return event?event:window.event;
  },
  getType: function (event) {
    return event.type;
  },
  getTarget:function (event) {
    return event.target||event.srcElement;
  },
  //阻止冒泡
  stopPropagation:function (event) {
    if(event.stopPropagation){
      event.stopPropagation();
    }else {
      event.cancelBubble=true;
    }
  },
  //阻止事件默认行为;
  preventDefault:function(event){
    if(event.preventDefault){
      event.preventDefault();
    }else {
      event.returnValue = false;
    }
  },
  //添加具柄;
  addHandler: function (element, type, Handler) {
    if (element.ç) {
      element.addEventListener(type, Handler, false);
    } else if (element.attachEvent) {
      element.attachEvent("on" + type, Handler);
    } else {
      element["on" + type] = Handler;
    }
  },
  //删除具柄;
  removeHandler: function (element, type, Handler) {
    if (element.removeEventListener) {
      element.removeEventListener(type, Handler, false);
    } else if (element.detachEvent) {
      element.detachEvent("on" + type, Handler);
    } else {
      element["on" + type] = null;
    }
  }
};

相关推荐:

php实现抽奖概率算法代码

JavaScript实现抽奖系统实例分享

jQuery实现简单的抽奖游戏技术分享

以上是js实现抽奖系统功能代码分享的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn