javascript抽奖代码,jquery抽奖的代码

本文目录一览:

js转盘抽奖求解释里面部分代码的原理

应该是界面效果切换代码

costheta = Math.cos(rad);

sintheta = Math.sin(rad);

是js数学函数

el.style.filter 是IE的滤镜函数,其他浏览器应该无效的

window.navigator.userAgent.indexOf(“Chrome”)=1)

是判断当前使用的浏览器函数,根据不同浏览器调用不同代码来实现效果

其他的不太清楚了

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

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

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

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

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

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做一个简单的抽奖程序

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

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随机抽奖一二三等奖不重复的抽奖逻辑怎么设置

!DOCTYPE html

html

head

titlejs随机抽奖一二三等奖不重复的抽奖逻辑怎么设置/title

meta charset=”UTF-8″ /

script

//添加一个随机函数

Math.rand = function(min, max){

function subRand(min, max){

min = min ? min : 0;

max = max ? max : 9;

var result = 0;

do{

result = Math.random().toString().substr(2, 1);

}while(!(result = min  result = max));

return result;

}

function getBit(maxBit){

maxBit = maxBit ? maxBit : max.toString().length;

var result = [],

count = 0;

for(var i = 0; i  maxBit; i++){

result.push(subRand());

if(i != 0  result[i] == result[i – 1]){

count++;

}

}

return maxBit – count;

}

min = min ? min : 0;

max = max ? max : 0;

var result = ”,

bit = getBit();

do{

result = ”;

for(var i = 0; i  bit; i++){

result = result + subRand();

}

result = parseInt(result);

}while(!(result = min  result = max));

return result;

}

//console.log(Math.rand(0, 100));

//LuckDraw 抽奖类 参数一 奖池数组, 参数二 中奖数量 返回 中奖索引

function LuckDraw(pool, numberOfWinners){

var results = [],

//是否已经中奖

isExists = function(index){

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

if(results[i] == index){

return true;

}

}

return false;

},

subLuckDraw = function(){

do{

result = Math.rand(0, pool.length – 1);

}while(isExists(result));

return result;

}

for(var i = 0; i  numberOfWinners; i++){

results.push(subLuckDraw());

}

return results;

}

var pool = [

‘关’,

‘张’,

‘赵’,

‘马’,

‘黄’,

‘曹老板’

];

winners = LuckDraw(pool, 3); //返回从奖池中 中奖的索引,假设123等奖都只有一名的话各取一个即可

//假设123等奖 一等奖 1名, 二等奖 2名, 三等奖 3名, 只需要调用这个函数第二个参数设置为总和 6 即可

//然后从结果中第一个索引为一等奖 23 索引为二等奖 , 456索引为 三等奖

console.log(winners);

/script

/head

body

script

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

document.write(pool[winners[i]] + ‘ 恭喜你中了’ + (i + 1) + ‘等奖br /’);

}

/script

/body

/html

原创文章,作者:BZRR,如若转载,请注明出处:https://www.506064.com/n/132034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
BZRRBZRR
上一篇 2024-10-03 23:49
下一篇 2024-10-03 23:49

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • Python基础代码用法介绍

    本文将从多个方面对Python基础代码进行解析和详细阐述,力求让读者深刻理解Python基础代码。通过本文的学习,相信大家对Python的学习和应用会更加轻松和高效。 一、变量和数…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • 仓库管理系统代码设计Python

    这篇文章将详细探讨如何设计一个基于Python的仓库管理系统。 一、基本需求 在着手设计之前,我们首先需要确定仓库管理系统的基本需求。 我们可以将需求分为以下几个方面: 1、库存管…

    编程 2025-04-29
  • 写代码新手教程

    本文将从语言选择、学习方法、编码规范以及常见问题解答等多个方面,为编程新手提供实用、简明的教程。 一、语言选择 作为编程新手,选择一门编程语言是很关键的一步。以下是几个有代表性的编…

    编程 2025-04-29
  • Python实现简易心形代码

    在这个文章中,我们将会介绍如何用Python语言编写一个非常简单的代码来生成一个心形图案。我们将会从安装Python开始介绍,逐步深入了解如何实现这一任务。 一、安装Python …

    编程 2025-04-29
  • 怎么写不影响Python运行的长段代码

    在Python编程的过程中,我们不可避免地需要编写一些长段代码,包括函数、类、复杂的控制语句等等。在编写这些代码时,我们需要考虑代码可读性、易用性以及对Python运行性能的影响。…

    编程 2025-04-29
  • Python爱心代码动态

    本文将从多个方面详细阐述Python爱心代码动态,包括实现基本原理、应用场景、代码示例等。 一、实现基本原理 Python爱心代码动态使用turtle模块实现。在绘制一个心形的基础…

    编程 2025-04-29
  • 北化教务管理系统介绍及开发代码示例

    本文将从多个方面对北化教务管理系统进行介绍及开发代码示例,帮助开发者更好地理解和应用该系统。 一、项目介绍 北化教务管理系统是一款针对高校学生和教职工的综合信息管理系统。系统实现的…

    编程 2025-04-29

发表回复

登录后才能评论