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程序 ...
随机推荐
- 在Windows端安装kafka 提示错误: 找不到或无法加载主类 的解决方案
在配置好kafka的server.properties文件后,cmd进入命令窗口输入命令:.\bin\windows\kafka-server-start.bat config\server.prop ...
- 67、Fragment实现Tab
<LinearLayout .......... <FrameLayout android:id="@+id/id_content" android:layout_wi ...
- 《从零开始学Swift》学习笔记(Day 57)——Swift编码规范之注释规范:文件注释、文档注释、代码注释、使用地标注释
原创文章,欢迎转载.转载请注明:关东升的博客 前面说到Swift注释的语法有两种:单行注释(//)和多行注释(/*...*/).这里来介绍一下他们的使用规范. 1.文件注释 文件注释就在每一个文件开头 ...
- VMware虚拟机NAT(地址转换模式)
转载于:https://www.linuxidc.com/Linux/2016-09/135521p2.htm 二.NAT(地址转换模式) 刚刚我们说到,如果你的网络ip资源紧缺,但是你又希望你的 ...
- [Algorithms] KMP
KMP is a classic and yet notoriously hard-to-understand algorithm. However, I think the following tw ...
- nvm-windows 手动安装 nvm use 无效 'node' 不是内部或外部命令,也不是可运行的程序
按照这两位前辈的教程,安装了nvm-windows. http://www.cnblogs.com/yesyes/p/7403184.html http://blog.csdn.net/jingsi1 ...
- logging/re - 总结
logging 模块 很多程序都有记录日志的需求 logging的日志可以分为 debug(), info(), warning(), error() and critical()5个级别 1.输出到 ...
- win7下设置smtp的方法
在win7中开启SMTP服务的方法如下: 1. 首先确定是否开启IIS服务:在运行中输入“inetmgr”,若提示出错,则表明未安装.进入步骤2,否则进入步骤3: 2.打开控制面板->程序-&g ...
- Linux上free命令的输出及其他
一.明确概念 A buffer is something that has yet to be "written" to disk. A cache is something t ...
- node.js---sails项目开发(5)---用户表的建立
1. ctrl+c 暂停sails项目 ,输入如下命令,创建一个user表 sails generate api user 2.在api目录分别建立了两个文件 api/controllers/U ...