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程序 ...
随机推荐
- 《ASP.NET 1200例》ref关键字与out关键字
REF关键字 ref 关键字会导致通过引用传递的参数,而不是值. 通过引用传递的效果是在方法中对参数的任何改变都会反映在调用方的基础参数中. 引用参数的值与基础参数变量的值始终是一样的. 不要将“通过 ...
- shop++源码反编译----随笔
一.applicationContext-mvc.xml配置 1.读取配置文件 <context:property-placeholder location="classpath*:/ ...
- 网络编程3 网络编程之缓冲区&subprocess&粘包&粘包解决方案
1.sub简单使用 2.粘包现象(1) 3.粘包现象(2) 4.粘包现象解决方案 5.struct学习 6.粘包现象升级版解决方案 7.打印进度条
- 第二课——解析mysqldump命令和mysqlbinlog命令+innodb和Myisam存储引擎简介
环境说明 mysql版本:Percona-Server-5.6.30 IP:10.7.15.167 端口:3306 安装目录:/httx/run/mysql 数据目录:/httx/run/mysql/ ...
- Exchange 2016 系统要求
Exchange 2016 和早期版本的 Exchange Server 共存方案 Exchange 2016支持混合部署方案 Exchange 2016 的网络和目录服务器要求 目录服务体系结构: ...
- 天天QA
w 0-Qphp 在全局范围内访问变量有哪几种方法A2种 <?php$a = 1;$b = 2;var_dump($GLOBALS); <?php$a = 1;$b = 2; functi ...
- Redis、MongoDB及Memcached的区别 Redis(内存数据库)
Redis.MongoDB及Memcached的区别 Redis(内存数据库) 是一个key-value存储系统(布式内缓存,高性能的key-value数据库).和Memcached类似,它支持存储的 ...
- sql server等待类型
sql server 各种等待类型-转 sql server各个等待类型及解决办法:https://www.sqlskills.com/help/waits/writelog/其他等待类型的描述和解决 ...
- node.js---sails项目开发(3)
1.为新建的sails数据库新建一个用户,首先连接数据库 mongo localhost:27017 (1)显示所有数据库 (2)切换数据库 show dbs use sails 新建一个用户 账 ...
- Java并发—java.util.concurrent并发包概括(转载)
一.描述线程的类:Runable和Thread都属于java.lang包 二.内置锁synchronized属于jvm关键字,内置条件队列操作接口Object.wait()/notify()/noti ...