网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

javascript抽奖,javascript抽奖代码多行

使用JavaScript完成一个抽奖程序,当单击页面上开始抽奖按钮时,在1~36中选取7个互不相同

1~36个数取7个不同的,需要用while循环+随机数去取出7个数。

为平乡等地区用户提供了全套网页设计制作服务,及平乡网站建设行业解决方案。主营业务为网站设计、成都网站建设、平乡网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

在while循环里面,可以判断取出来的数(用switch),是否是28,18,8. 如果有输出相应的奖项,如果没有,就输出其它的。然后将这7个数,打印出来就好了。

JavaScript(缩写为JS)是一种高级的、多范式、解释型的编程语言,是一门基于原型、函数先行的语言,它支持面向对象编程、命令式编程以及函数式编程。

它提供语法来操控文本、数组、日期以及正则表达式,不支持I/O(比如网络、存储和图形等),但可以由它的宿主环境提供支持。它已经由ECMA(欧洲计算机制造商协会)通过ECMAScript实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器支持。

如何使用javascript做一个简单的抽奖程序

参考下面写的小程序,是一个跑马灯效果。

html

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

title抽奖/title

style

*{margin:0;padding:0;}

#box{width:400px;height:400px;border:2px #C6C solid;margin:20px auto;background:#99F;}

#boxdiv{position:absolute;width:60px;height:40px;color:#F03;font-size:20px;border:2px #F63 solid;opacity:0.4;line-height:40px;background:#F96;}

#butt{position:absolute;background:#F6C;color:#36F;font-size:16px;margin-top:200px;margin-left:150px;}

#span{width:300px;height:40px;margin-left:160px;display:block;border:2px #F63 solid;float:left;line-height:40px;}

#span i{font-style:normal;}

/style

/head

body

span id="span"离抽奖结束还有:i/ii/ii/ii/i/span

div id="box" class="box"

div一等奖/div

div style="margin-left:336px"二等奖/div

div style="margin-left:336px;margin-top:356px;"三等奖/div

div style="margin-top:356px;"安慰奖/div

button id="butt" onclick="butt()"点击抽奖/button

/div

script type="text/javascript"

var opa=document.getElementById("box").getElementsByTagName("div");

var ii=document.getElementById("span").getElementsByTagName("i");

for(var i=0;iopa.length;i++){

opa[i].style.opacity=0.4;

}

//var span=document.getElementById("span").style;

var i=0,

t,tt,r,

count1=1,//限制一等奖只中一次,当一等奖抽完后顺延到二等奖

count2=3,//限制二等奖只中3次,当二等奖抽完后顺延到三等奖

count3=10,//限制等奖只中10次

prize=1,//抽奖是否结束

time=200,

curr=0;//每次抽奖完成才能再次点击

var toDate = new Date(2016,7,29,19,55,0);

var dt,currDate;

function obtain(){//倒计时

currDate=new Date();

dt=Math.ceil((toDate.getTime()-currDate.getTime())/1000);!--转换成s--

ii[0].innerHTML=parseInt(dt/(24*60*60))+'天';

ii[1].innerHTML=Math.floor(dt%(24*60*60)/(60*60))+'时';

ii[2].innerHTML=Math.floor(dt%(24*60*60)%(60*60)/60)+'分';

ii[3].innerHTML=Math.floor(dt%60)+'秒';

if(dt=0){

prize=0;

ii[0].innerHTML=0+'天';ii[1].innerHTML=0+'时';ii[2].innerHTML=0+'分';ii[3].innerHTML=0+'秒';

if(prize==0s==0){alert("抽奖已结束");clearInterval(tt);}

}

}

tt=setInterval(obtain,1000);

function butt(){

curr++;

if(count1==0count2==0count3==0){

prize=0;

}

if(curr==1prize==1){

if(i!=0){

opa[i].style.opacity=0.4;

}

i=0;

clearInterval(t);

r=parseInt(Math.random()*10000)+1;//产生1-10000的随机数

//r=1;

opa[i].style.opacity=1;

t=setInterval(move,time);

}else if(curr!=1prize==1){alert("请等待本次抽奖完成再抽奖");alert("请点击确定继续");}

else if(prize==0){alert("抽奖已结束");}

}

/*function move(r){

switch(r){

case 1:

}

}*/

function move(){

opa[i].style.opacity=0.4;

if(iopa.length-1){

i++;

}else{i=0;}

opa[i].style.opacity=1;

time+=50;

console.log("r="+r+","+"time="+time);

if(r==1count10){//中一等奖

clearInterval(t);

t=setInterval(move,time);

if(time==800){

clearInterval(t);

time=200;

count1--;

curr=0;

console.log("中一等奖的次数还有:"+count1+"次");

}

}else if((r%2999==0||r==1count1==0)count20){//中二等奖

clearInterval(t);

t=setInterval(move,time);

if(time==850){

count2--;

clearInterval(t);

time=200;

curr=0;

console.log("中二等奖的次数还有:"+count2+"次");

}

}else if((r%1000==0||r%2999==0count2==0||r==1count1==0)count30){//中三等奖

clearInterval(t);

t=setInterval(move,time);

if(time==900){

time=200;

count3--;

clearInterval(t);

curr=0;

console.log("中三等奖的次数还有:"+count3+"次");

}

}else{//安慰奖

clearInterval(t);

t=setInterval(move,time);

if(time==950){

clearInterval(t);

curr=0;

time=200;

}

}

}

/script

/body

/html

js实现可键盘控制的简单抽奖程序

本文实例为大家分享了js抽奖程序的编写代码,以及编写注意事项,感兴趣的小伙伴们可以参考一下

代码:

!DOCTYPE

html

html

lang="en"

head

meta

charset="UTF-8"

title简单抽奖(可用键盘)/title

style

*{margin:0;padding:0;}

.box{width:

400px;height:

300px;margin:50px

auto;background:

red}

.title{color:

#fff;font-size:

30px;font-weight:700px;padding:

50px

0;text-align:

center;height:40px;}

.btm{text-align:

center;padding:20px

0;}

.btm

a{display:

inline-block;width:

120px;height:60px;line-height:

60px;background:

#FEF097;margin:0

10px;text-decoration:

none;}

/style

script

var

data=['Iphone','Ipad','笔记本','相机','谢谢参与','充值卡','购物券'],

timer=null,//定时器

flag=0;//阻止多次回车

window.onload=function(){

var

play=document.getElementById('play'),

stop=document.getElementById('stop');

//

开始抽奖

play.onclick=playFun;

stop.onclick=stopFun;

//

键盘事件

document.onkeyup=function(event){

event

=

event

||

window.event;

//

回车键的code值:13

if(event.keyCode==13){

if(flag==0){

playFun();

flag=1;

}else{

stopFun();

flag=0;

}

}

}

function

playFun(){

var

title=document.getElementById('title');

var

play=document.getElementById('play');

clearInterval(timer);

timer=setInterval(function(){

var

random=Math.floor(Math.random()*data.length);

title.innerHTML=data[random];

},60);

play.style.background='#999';

}

function

stopFun(){

clearInterval(timer);

var

play=document.getElementById('play');

play.style.background='#FEF097';

}

}

/script

/head

body

div

class="box"

div

class="title"

id="title"淘家趣抽奖/div

div

class="btm"

a

href="javascript:;"

id="play"开始/a

a

href="javascript:;"

id="stop"停止/a

/div

/div

/body

/html

注意点:

1.随机数,取数组的其中一个;取0-n之间:Math.random()*(n+1)

2.定时器,开始抽奖时要停止前面的一次抽奖,不然会定时器重叠

3.按键操作,要判断是抽奖进行中,还是未开始,所有设置了变量

flag

想要学习更多关于javascript抽奖功能,请参考此专题:javascript实现抽奖功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


标题名称:javascript抽奖,javascript抽奖代码多行
网页地址:http://bjjierui.cn/article/dsdjcdi.html

其他资讯