本文目錄一覽:
- 1、js轉盤抽獎求解釋裡面部分代碼的原理
- 2、使用JavaScript完成一個抽獎程序,當單擊頁面上開始抽獎按鈕時,在1~36中選取7個互不相同
- 3、js實現可鍵盤控制的簡單抽獎程序
- 4、如何使用javascript做一個簡單的抽獎程序
- 5、js隨機抽獎一二三等獎不重複的抽獎邏輯怎麼設置
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/zh-hant/n/132034.html