js简单抽奖代码

2019-12-18,,

核心:js的Math对象和Array对象

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>random</title>
 <style>
  #awardListDom{width: 100%;}
 </style>
</head>
<body>
 <label for="awardListDom">奖项列表</label><br>
 <input type="text" value="" id="awardListDom"> <br>
 <label for="num">抽到的奖</label><br>
 <input type="text" value="" id="num"> <br>
 <button id="submit">开始抽奖</button>
 <script>
  /* 
  * 思路:随机抽奖,抽一个奖项便减少一个
  * Math 对象方法:http://www.w3school.com.cn/jsref/jsref_obj_math.asp
  *  -random():返回 0 ~ 1 之间的随机数。
  *  -floor():获取整数
  * 数组操作:
  *  - splice(x,y); x:起始位置, y:获取并删除个数
  */

  function random(min,max){
    return Math.floor(min+Math.random()*(max-min));
  }
  var awardListDom=document.getElementById("awardListDom"),
  num=document.getElementById("num"),
  submit=document.getElementById("submit");
  var awardList=["一等奖","二等奖","二等奖","三等奖","三等奖","三等奖","鼓励奖","鼓励奖","鼓励奖","鼓励奖","谢谢参与","谢谢参与","谢谢参与","谢谢参与","谢谢参与","谢谢参与"];

  awardListDom.value=awardList;
  submit.onclick=function(){
   //引用数组
   var oldArray=awardList;
   var rNum=random(0,oldArray.length);
   
   if(oldArray.length<1){
    awardListDom.value="活动结束";
    num.value="活动结束";
   }
   else{
    num.value=oldArray[rNum];
    oldArray.splice(rNum,1);
    awardListDom.value=oldArray;
   }
  }
 </script>
</body>
</html>

demo:http://demo.jb51.net/js/2015/choujiang/

github:https://github.com/litengdesign/award

您可能感兴趣的文章:

  • js和html5实现手机端刮刮卡抽奖效果完美兼容android/IOS
  • js轮盘抽奖实例分析
  • js抽奖实现随机抽奖代码效果
  • jquery.rotate.js实现可选抽奖次数和中奖内容的转盘抽奖代码
  • js实现简单随机抽奖的方法
  • js实现大转盘抽奖游戏实例
  • javascript 随机抽奖程序代码
  • 原生JS实现九宫格抽奖效果
  • JS模拟抽奖序效果实现代码
  • 原生js实现抽奖小游戏

《js简单抽奖代码.doc》

下载本文的Word格式文档,以方便收藏与打印。