直接上代码

<style>
*{ margin: 0; padding:0;}
.prize_wrap{
width: 300px;
height: 150px;
}
.prize_wrap .active{
position: absolute;
width: 100px;
height: 50px;
background: #f00;
line-height: 50px;
text-align: center;
border:solid 1px #999;
margin:-1px;
}
.prize_cell,
.prize_btn{
position: absolute;
background:#ccc;
width: 100px;
height: 50px;
line-height: 50px;
text-align: center;
}
.prize_btn{
background:#f0f;
cursor: pointer;
}
</style>
<div class="prize_wrap" id="prize_wrap">
<div class="prize_cell" style="left:0;top:0">1</div>
<div class="prize_cell" style="left:100px;top:0">2</div>
<div class="prize_cell" style="left:200px;top:0">3</div>
<div class="prize_cell" style="left:200px;top:50px">4</div>
<div class="prize_btn" style="left:100px;top:50px" id="prize_start">抽奖开始</div>
<div class="prize_cell" style="left:200px;top:100px">5</div>
<div class="prize_cell" style="left:100px;top:100px">6</div>
<div class="prize_cell" style="left:0;top:100px">7</div>
<div class="prize_cell" style="left:0;top:50px">8</div> </div>
<script>
//构造函数
var prizeScroll=(function(doc){
function _getItemsFilterCls(cls,items){
var ret=[];
for(var i=0,len=items.length;i<len;i++){
if(items[i].className.indexOf(cls)>-1){
ret.push(items[i]);
}
}
return ret;
}
return function(opt){
this.wrap=doc.getElementById(opt.id)||doc.body;
this.items=opt.items||_getItemsFilterCls(opt.id.replace('_wrap','')+'_cell',this.wrap.getElementsByTagName('*'));
this.btn=doc.getElementById(opt.btn);
this.curClass=opt.curClass||'active';
this.prizeNum=-1;
this.curIdx=0;
this.interval=null;
this.queue=[100,50,30,50,100,150];//转速
this.queue.gid=0;//转圈数
this.callBack=opt.callBack||function(){};
this.init();
}
})(document); //工具方法
prizeScroll.log=function(msg){
if(console&&console.log){
console.log(msg);
}else{
alert(msg);
}
} prizeScroll.prototype.init=function(){
//初始化
//prizeScroll.log('init');
} prizeScroll.prototype._setInterval=function(timer,stopNum){
//滚动动画
var _self=this,len=stopNum||_self.items.length;
_self._clearInterval();
_self.interval=setInterval(function(){
if(_self.curIdx>len-1){
_self._clearInterval();
_self._next();
return;
}
_self._setActive(_self.curIdx);
_self.curIdx++;
},timer);
}
prizeScroll.prototype._setActive=function(idx){
//设置中奖状态
for(var i=0,len=this.items.length;i<len;i++){
this.items[i].className='prize_cell';
}
this.items[idx].className='active';
} prizeScroll.prototype._clearInterval=function(){
//清除动画
var _self=this;
_self.interval&&clearInterval(_self.interval);
}
prizeScroll.prototype.start=function(){
//开始
this._next();
}
prizeScroll.prototype._next=function(){
//动画排序
this.curIdx=0;
this.interval=null;
var time=this.queue[this.queue.gid];
if(this.queue.gid>this.queue.length-1){
this.callBack(this.prizeNum);
return;}
if(this.queue.gid===this.queue.length-1){
this._setInterval(time,this.getPrizeNum());//奖品设置
this.queue.gid++;
return;
}
this._setInterval(time);
this.queue.gid++;
} prizeScroll.prototype.reset=function(){
//重置重新开始
this.stop();
this.queue.gid=0;
}
prizeScroll.prototype.getPrizeNum=function(){
//获取奖品号
return this.prizeNum;
}
prizeScroll.prototype.setPrizeNum=function(prizeNum){
//设置奖品号
this.prizeNum=prizeNum;
}
prizeScroll.prototype.stop=function(){
//停止
//prizeScroll.log('stop');
this._clearInterval();
} var prizeAssembly={
"1":"**币10枚",
"2":"**币110枚",
"3":"**币10枚",
"4":"**币130枚",
"5":"**币160枚",
"6":"**币20枚",
"7":"**币60枚",
"8":"**币00枚"
} var m=new prizeScroll({
id:'prize_wrap',
callBack:function(prizeNum){
alert("您中了 "+prizeAssembly[prizeNum]+" ");
}
})
document.getElementById('prize_start').onclick=function(){
m.setPrizeNum(1);
m.start();
//移除事件
document.getElementById('prize_start').onclick=null;
} </script>

其中奖品结果,奖品信息,可配置,方便修改。 

javascript-实现小抽奖程序的更多相关文章

  1. 【转】为 OSCHINA 聚会搞的一个小抽奖程序

    http://www.oschina.net/code/snippet_12_7605 在线演示: http://www.oschina.net/r.html

  2. 使用 JavaScript 中的 window 对象执行计时操作,完成一个抽奖程序

    查看本章节 查看作业目录 需求说明: 使用 JavaScript 中的 window 对象执行计时操作,完成一个抽奖程序 实现思路: 在 HTML 页面中放置背景图片.转盘和指针 3 张图片,并设置指 ...

  3. 简单的javascript抽奖程序

    <html>  <head>   <title>手机号码抽奖程序</title>   <script>    //声明一个数组装住号码,可根 ...

  4. javascript实现抽奖程序

    昨天开年会的时候看到一个段子说唯品会年会抽奖,结果大奖都被写抽奖程序的部门得了,CTO现场review代码. 简单想了一下抽奖程序的实现,花了十几分钟写了一下,主要用到的知识有数组添加删除,以及ES5 ...

  5. 【JavaScript定时器小案例】常见的几种定时器实现的案例

    [JavaScript定时器小案例]常见的几种定时器实现的案例 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 在日常开发 ...

  6. jQuery幸运大转盘_jQuery+PHP抽奖程序的简单实现

    jQuery幸运大转盘_jQuery+PHP抽奖程序的简单实现 在线实例 查看演示 完整代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

  7. 一个好玩的jq+php实现转盘抽奖程序

    前台页面: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <met ...

  8. sql的行转列(PIVOT)与列转行(UNPIVOT) webapi 跨域问题 Dapper 链式查询 扩展 T4 代码生成 Demo (抽奖程序)

    sql的行转列(PIVOT)与列转行(UNPIVOT)   在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻烦了,而且可扩展性不强,可以使用 PIVOT,UNPIVOT比 ...

  9. javascript版的quine程序-返回自身源码

    引用自Wikipedia: 一个quine是一个计算机程序,它不接受任何输入,且唯一的输出就是自身的源代码. @cowboy (Ben Alman) 给出了一个用JavaScript写的quine程序 ...

随机推荐

  1. Ubuntu14.04编译Android5.1.1源码

    1.Ubuntu环境配置 硬盘:120G 内存:4G 注:配置很重要,直接影响能否编译成功. 2.安装JDK sudo apt-get update sudo apt-get install open ...

  2. 71、Android上对Cookie的读写操作

    Cookie是为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据,在Android中也经常用到,接下来我们介绍Cookie在Android里是如何进行读写的.   Cookie其实就 ...

  3. 并发编程5 操作系统&进程

    一.今日大纲 1.multiprocessing模块简单应用 2.for循环创建进程 3.进程传参方式和创建方式2 4.join方法 5.操作系统基础 二.今日内容 (1)操作系统简单介绍 多道技术: ...

  4. C Plus Plus 基础

    C Plus Plus 基础 一.变量和基本类型 1.const 限定符号 const --> constant(中文意思:不停的,不断的,一直不变的) ①代替Magic Number(即『无意 ...

  5. Android 点击电话号码之间拨号

    点击电话号码之间拨打电话,可用通过下面的方式实现: 假设电话号码以TextView的方式显示 1.Intent方式 在TextView的响应事件中 : String phone = tvphone.g ...

  6. epoll浅析以及nio中的Selector

    出处: https://my.oschina.net/hosee/blog/730598 首先介绍下epoll的基本原理,网上有很多版本,这里选择一个个人觉得相对清晰的讲解(详情见reference) ...

  7. Gson 解析多层嵌套JSON数据

    http://stackoverflow.com/questions/14139437/java-type-generic-as-argument-for-gson

  8. java配置文件properties,yml,一般文件

    JAVA编写配置文件的几种方式: JAVA配置文件,一般都放在resource目录下,无论是下面提到的properties.yml还是普通的txt等文件. 在打成jar包之后,只需要jar包程序就可运 ...

  9. Redis与Memcached的比较(转)

    原文:http://blog.nosqlfan.com/html/3729.html 这两年Redis火得可以,Redis也常常被当作Memcached的挑战者被提到桌面上来.关于Redis与Memc ...

  10. kubernetes,Docker网络相关资料链接

    1.Why kubernetes not doesn't use libnetwork http://blog.kubernetes.io/2016/01/why-Kubernetes-doesnt- ...