JS数字抽奖游戏实现方法


Posted in Javascript onMay 04, 2015

本文实例讲述了JS数字抽奖游戏实现方法。分享给大家供大家参考。具体实现方法如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>新年网页抽奖程序</title>
<style type="text/css">
* {margin:0; padding:0;}
ul,li {list-style-type:none;}
body {overflow:hidden;}
#back {width:100%; height:100%;
background:#f5f5f5; position:absolute; z-index:1;
}
#box {width:360px; height:100px;
position:absolute; z-index:3; top:50%; left:50%;
margin-top:-50px; margin-left:-180px; text-align:center;
}
#box1,#box2,#box3 {width:100px; height:100px;
line-height:100px;
float:left; background:#321c24; 
border:10px #321c24 solid;
border-radius:50%; position:relative; overflow:hidden;
}
#box1 ul,#box2 ul,#box3 ul {color:#fff; font-size:68px; 
font-family:"Arial Black"; text-align:center;
width:100px; height:100px; line-height:100px;
position:absolute; top:0; left:0;
}
#box1 ul li,#box2 ul li,#box3 ul li {
width:100px; height:100px;
background:red; border-radius:50%;
}
</style>
<script type="text/javascript">
var AIR = {
 $: function (id)
 {
  return typeof id === "string" ? document.getElementById(id) : id;
 }, 
 $$: function (elem, oParent)
 {
  return (oParent || document).getElementsByTagName(elem);
 },
 addEvent: function (oElement, sEvent, fnHandler) 
 {
  oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler) 
 },
 removeEvent: function (oElement, sEvent, fnHandler) 
 {
  oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)
 }, 
 getElementClient: function (){
  var arr = [];
  if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth){
   arr.push(document.documentElement.clientWidth);
   arr.push(document.documentElement.clientHeight);
   return arr;
  }
 },
 getStyle: function (obj, attr)
 {
  return parseFloat(obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, null)[attr])
 },
 startMove: function (obj, pos, onEnd)
 {
  clearInterval(obj.timer);
  var _this = this;
  obj.timer = setInterval(function ()
  {
   _this.doMove(obj, pos, onEnd)
  }, 30) 
 },
 doMove: function (obj, pos, onEnd)
 {
  var iCurL = this.getStyle(obj, "left");
  var iCurT = this.getStyle(obj, "top");
  var iSpeedL = (pos.left - iCurL) / 5;
  var iSpeedT = (pos.top - iCurT) / 5;
  iSpeedL = iSpeedL > 0 ? Math.ceil(iSpeedL) : Math.floor(iSpeedL);
  iSpeedT = iSpeedT > 0 ? Math.ceil(iSpeedT) : Math.floor(iSpeedT);
  if (pos.left == iCurL && pos.top == iCurT)
  {
   clearInterval(obj.timer);
   onEnd && onEnd()
  }
  else
  {
   obj.style.left = iCurL + iSpeedL + "px";
   obj.style.top = iCurT + iSpeedT + "px"; 
  }
 }
}
function Draw (obj, num)
{
 this.obj = obj;
 this.num = num;
 this.data = [];
 this.result = [];
 this.show = 0;
 this.btn = true;
 this.timer = true;
 this.h = 0;
 this.uh = 0;
 this.initialize();  
}
Draw.prototype = {
 initialize: function ()
 {
  this.createArr ();
  this.createElement ();
  this.closeEvent ();
  this.startDraw (); 
 },
 createElement: function ()
 {
  for(var j=0; j<this.obj.length; j++){ 
   var ul = document.createElement("ul");
   for(var i=0; i<10; i++){
    var li = document.createElement("li");
    li.innerHTML = i;
    ul.appendChild(li) 
   } 
   this.obj[j].appendChild(ul);
   this.obj[j].btn = true;
   AIR.$$("ul",this.obj[j])[0].innerHTML += AIR.$$("ul",this.obj[j])[0].innerHTML;  
  } 
  var UL = AIR.$$("ul",this.obj[0])[0];
  this.h = AIR.getStyle(AIR.$$("li",UL)[0],"height");
  this.uh = AIR.$$("li",UL).length * this.h
 },
 randomSort: function (a, b) {
  return Math.random()>.5 ? -1 : 1;
 },
 createArr: function ()
 {
  for(var i=0; i<this.num+1; i++){
   this.data.push(i);   
  } 
  this.data.sort(this.randomSort); 
 },
 closeEvent: function ()
 {
  document.onmousedown=document.onmousemove=document.oncontextmenu=function()
  {
   return false; 
  }  
 },
 startDraw: function ()
 {
  var _this = this;
  document.onkeyup = function ( ev )
  {
   var ev = ev || window.event;
   if(ev.keyCode == 13 || ev.keyCode == 32){
    if(_this.btn && _this.timer){
     if(_this.obj[_this.obj.length-1].btn){
      _this.Play ();
      _this.btn = !_this.btn;
      _this.timer = !_this.timer; 
     }      
    }else{
     if(_this.obj[_this.obj.length-1].btn){
      _this.Stop ();
      _this.btn = !_this.btn;
      _this.timer = !_this.timer; 
     }
    }
    return false;
   }else{
    return false; 
   }
  }
 },
 Play: function ()
 {
  if(this.timer && this.btn){
   var t = 0;
   for(var i=0; i<this.obj.length; i++){
    this.obj[i].btn = false;
    this.playTimer (this.obj[i],t); 
    t += 1500;
   }
  }else{
   return false; 
  }
 },
 playTimer: function (obj,t)
 {
  var _this = this;
  setTimeout(function(){
   _this.Move (obj);
  },t) 
 },
 Del: function (a)
 {
  for(var i=0; i<this.data.length; i++){
   if(a == this.data[i]){
    this.data.splice(i,1); 
   } 
  } 
 },
 Stop: function ()
 {
  if(!this.timer && !this.btn){
   var n = this.num + 1;
   var r = this.data[Math.floor(Math.random() * (0-n) + n)];
   this.show = r;
   this.Del (r);
   r = r.toString().split("");
   var c = this.obj.length - r.length;
   if(r.length < this.obj.length){
    for(var i=0; i<c; i++){
     r.unshift(0) 
    } 
   }
   this.result = r; 
   //document.title = r+" : "+this.data; 
   var t = 0;
   for(var i=0; i<this.obj.length; i++){
    this.obj[i].btn = false;
    this.obj[i].index = i;
    this.obj[i].num = this.result[this.obj[i].index];
    this.stopTimer (this.obj[i],t); 
    t += 1500;
   }
  }
 },
 stopTimer: function (obj,t)
 {
  var _this = this;
  setTimeout(function(){
   _this.showResult (obj);
  },t)
 },
 showResult: function (obj)
 { 
  var _this = this;
  this.timer = true;
  this.btn = true;
  obj.btn = false;
  obj.vh = -obj.num * this.h;
  obj.timeOut = setInterval(function(){
   obj.speed -= 1;
   if(obj.speed == 1){
    clearInterval(obj.timeOut); 
    clearInterval(obj.timer);
    obj.timer = setInterval(function(){
     if(obj.ul.offsetTop >= obj.vh){
      clearInterval(obj.timer);
      AIR.startMove(obj.ul,{left:0,top:obj.vh},function(){
       obj.btn = true; 
       var set = true;
       for(var i=0; i<_this.obj.length; i++){
        if(!_this.obj[i].btn){
         set = false;  
        }
       }
       if(set){
        _this.Open(_this.show) 
       }
      });
     }
     obj.ul.style.top = obj.ul.offsetTop + obj.speed +"px"; 
    },30);
   }
  },100);  
 },
 Open: function (num)
 {
  document.title += " "+ num;
 },
 Move: function (obj)
 {
  var _this = this;
  var obj = obj;
  obj.btn = false;
  obj.timer = null;
  obj.speed = 1;
  obj.ul = AIR.$$("ul",obj)[0];
  obj.ul.style.height = this.uh +"px";
  obj.timer = setInterval(function(){
   if(obj.ul.offsetTop > 0){
    obj.ul.style.top = -(_this.uh/2) +"px";
   }
   obj.ul.style.top = obj.ul.offsetTop + obj.speed +"px"; 
  },30);
  obj.timeOut = setInterval(function(){
   obj.speed += 1;
   if(obj.speed == 30){
    clearInterval(obj.timeOut);
    obj.btn = true; 
   }
  },300) 
 }
}
var initialize = function ()
{
 new Draw ([AIR.$("box1"),AIR.$("box2"),AIR.$("box3")],100);
 reSize ();
}
var reSize = function ()
{
 var v = AIR.getElementClient();
 AIR.$$("img",AIR.$("back"))[0].width = v[0];
 AIR.$$("img",AIR.$("back"))[0].height = v[1]; 
}
AIR.addEvent(window,"load",initialize);
AIR.addEvent(window,"resize",reSize);
</script>
</head>
<body>
<div id="box">
 <div id="box1"></div>
 <div id="box2"></div>
 <div id="box3"></div>
 <div style="clear:both"></div>
</div>
<div id="back">
 <img src="images/20153291274950386.jpg" />
</div>
<div id="showback">100</div>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
js跨域和ajax 跨域问题的实现思路
Sep 05 Javascript
jquery div拖动效果示例代码
Dec 08 Javascript
jQuery鼠标经过方形图片切换成圆边效果代码分享
Aug 20 Javascript
JS 拼凑字符串的简单实例
Sep 02 Javascript
jQuery实现边框动态效果的实例代码
Sep 23 Javascript
微信小程序 Template详解及简单实例
Jan 05 Javascript
Bootstrap 3浏览器兼容性问题及解决方案
Apr 11 Javascript
Vue原理剖析 实现双向绑定MVVM
May 03 Javascript
vue结合axios与后端进行ajax交互的方法
Jul 06 Javascript
解决vue select当前value没有更新到vue对象属性的问题
Aug 30 Javascript
js实现飞机大战游戏
Aug 26 Javascript
Javascript柯里化实现原理及作用解析
Oct 22 Javascript
JS实现跟随鼠标立体翻转图片的方法
May 04 #Javascript
js实现使用鼠标拖拽切换图片的方法
May 04 #Javascript
js实现每日自动换一张图片的方法
May 04 #Javascript
jQuery仿天猫实现超炫的加入购物车
May 04 #Javascript
JavaScript中操作字符串小结
May 04 #Javascript
JavaScript中常见的字符串操作函数及用法汇总
May 04 #Javascript
javascript多行字符串的简单实现方式
May 04 #Javascript
You might like
在数据量大(超过10万)的情况下
2007/01/15 PHP
简单的cookie计数器实现源码
2013/06/07 PHP
PHP生成Gif图片验证码
2013/10/27 PHP
php fsockopen解决办法 php实现多线程
2014/01/20 PHP
完美解决phpexcel导出到xls文件出现乱码的问题
2016/10/29 PHP
PHP字符串逆序排列实现方法小结【strrev函数,二分法,循环法,递归法】
2017/01/13 PHP
php使用socket调用http和smtp协议实例小结
2019/07/26 PHP
一段利用WSH获取登录时间的jscript代码
2008/05/11 Javascript
有关javascript的性能优化 (repaint和reflow)
2013/04/12 Javascript
js动态调用css属性的小规律及实例说明
2013/12/28 Javascript
Javascript 浮点运算精度问题分析与解决
2014/03/26 Javascript
js验证IP及子网掩码的合法性有效性示例
2014/04/30 Javascript
Javascript玩转继承(二)
2014/05/08 Javascript
用js模拟struts2的多action调用示例
2014/05/19 Javascript
Bootstarp风格的toggle效果分享
2016/02/23 Javascript
Javascript字符串拼接小技巧(推荐)
2016/06/02 Javascript
JS遍历ul下的li点击弹出li的索引的实现方法
2016/09/19 Javascript
深入理解JavaScript中的并行处理
2016/09/22 Javascript
基于vue2.0+vuex的日期选择组件功能实现
2017/03/13 Javascript
使用jquery的jsonp如何发起跨域请求及其原理详解
2017/08/17 jQuery
vue-cli如何引入bootstrap工具的方法
2017/10/19 Javascript
Linux Centos7.2下安装nodejs&amp;npm配置全局路径的教程
2018/05/15 NodeJs
利用js实现简易红绿灯
2020/10/15 Javascript
[03:01]2014DOTA2国际邀请赛 DC:我是核弹粉,为Burning和国土祝福
2014/07/13 DOTA
在Python中使用SQLite的简单教程
2015/04/29 Python
Swift 3.0在集合类数据结构上的一些新变化总结
2016/07/11 Python
python实现基于信息增益的决策树归纳
2018/12/18 Python
python selenium 查找隐藏元素 自动播放视频功能
2019/07/24 Python
python通过移动端访问查看电脑界面
2020/01/06 Python
Django中从mysql数据库中获取数据传到echarts方式
2020/04/07 Python
Python hashlib和hmac模块使用方法解析
2020/12/08 Python
英国网上香水店:Fragrance Direct
2016/07/20 全球购物
西班牙在线光学:Visual-Click
2020/06/22 全球购物
J2EE面试题大全
2016/08/06 面试题
介绍一下Ruby的多线程处理
2013/02/01 面试题
品牌推广策划方案
2014/05/28 职场文书