javascript-实现小抽奖程序
直接上代码
<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-实现小抽奖程序的更多相关文章
- 【转】为 OSCHINA 聚会搞的一个小抽奖程序
http://www.oschina.net/code/snippet_12_7605 在线演示: http://www.oschina.net/r.html
- 使用 JavaScript 中的 window 对象执行计时操作,完成一个抽奖程序
查看本章节 查看作业目录 需求说明: 使用 JavaScript 中的 window 对象执行计时操作,完成一个抽奖程序 实现思路: 在 HTML 页面中放置背景图片.转盘和指针 3 张图片,并设置指 ...
- 简单的javascript抽奖程序
<html> <head> <title>手机号码抽奖程序</title> <script> //声明一个数组装住号码,可根 ...
- javascript实现抽奖程序
昨天开年会的时候看到一个段子说唯品会年会抽奖,结果大奖都被写抽奖程序的部门得了,CTO现场review代码. 简单想了一下抽奖程序的实现,花了十几分钟写了一下,主要用到的知识有数组添加删除,以及ES5 ...
- 【JavaScript定时器小案例】常见的几种定时器实现的案例
[JavaScript定时器小案例]常见的几种定时器实现的案例 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 在日常开发 ...
- jQuery幸运大转盘_jQuery+PHP抽奖程序的简单实现
jQuery幸运大转盘_jQuery+PHP抽奖程序的简单实现 在线实例 查看演示 完整代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- 一个好玩的jq+php实现转盘抽奖程序
前台页面: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <met ...
- sql的行转列(PIVOT)与列转行(UNPIVOT) webapi 跨域问题 Dapper 链式查询 扩展 T4 代码生成 Demo (抽奖程序)
sql的行转列(PIVOT)与列转行(UNPIVOT) 在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻烦了,而且可扩展性不强,可以使用 PIVOT,UNPIVOT比 ...
- javascript版的quine程序-返回自身源码
引用自Wikipedia: 一个quine是一个计算机程序,它不接受任何输入,且唯一的输出就是自身的源代码. @cowboy (Ben Alman) 给出了一个用JavaScript写的quine程序 ...
随机推荐
- JDB调试之小试牛刀
用JDK自带工具JDB调试示例程序HelloJDB(d:\jdb\HelloJDB) HelloJDB代码如下: public class HelloJDB { public static void ...
- JZOJ.5246【NOIP2017模拟8.8】Trip
Description 多年之后,worldwideD厌倦竞争,隐居山林. 他的家乡开始发展起了旅游业,在一条很长的主干道上,有N个旅游景点,按顺序编号为1到N.根据游客们网 ...
- 【BZOJ1922】[Sdoi2010]大陆争霸 Dijkstra
Description 具体地说,杰森国有 N 个城市,由 M条单向道 路连接.神谕镇是城市 1而杰森国的首都是城市 N.你只需摧毁位于杰森国首都 的曾·布拉泽大神殿,杰森国的信仰,军队还有一切就都会 ...
- Kubernetes之kubectl常用命令
最近项目有用到Kubernetes作集群配置,所以学习下相关命令,记录下以备下次使用... kubectl help 显示具体的用法 kubectl controls the Kubernetes c ...
- 烂笔头-Spring3
1.spring相关jar包的导入 2.配置文件bean.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- java.lang.IllegalStateException: Queue full
其实异常说的很清楚 队列满了! ArrayBlockingQueue FIFO 的队列: ArrayBlockingQueue内部是通过一个Object数组和一个ReentrantLock实现的.同时 ...
- 动态长度中英字符串显示至固定高度td
w 为td中英字符串区域设置为display:block; height=td_height,并指明td width. <!doctype html> <html lang=&quo ...
- Struts2 框架的值栈
1. OGNL 表达式 1.1 概述 OGNL(Object Graphic Navigation Language),即对象图导航语言; 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个 ...
- Compilation failed: internal java compiler error
在Idea中编译时出现这个错误:Error:java: Compilation failed: internal java compiler error. Information:Using java ...