项目描述:九宫格的跑马灯抽奖特效,抽中奖品1-9的概率分别是2%,2%,4%,1%,12%,1%,8%,70%,概率总计100%,并且每天抽奖次数最多为3次。
html部分的代码和css部分的代码和上一节的一样,没有修改,js部分有修改,增加了概率。
原理很简单,同样是取随机数,判断这个随机数的大小在100的0-2,2-4,4-8,8-9,9-21,21-22,22-30,30-100之间的哪一个数组内,随机就可判断中哪个奖项。
代码在上一节的js的代码上面增加了一个maths_f()函数,如下:
function maths_f(){
if(maths>0&&maths<2){
console.log('恭喜获得奖品1')
twice=1;
}else if(maths>=2&&maths<=4){
console.log('恭喜获得奖品2')
twice=2;
}else if(maths>=4&&maths<8){
console.log('恭喜获得奖品3')
twice=3;
}else if(maths>=8&&maths<9){
console.log('恭喜获得奖品4')
twice=4;
}else if(maths>=9&&maths<21){
console.log('恭喜获得奖品5')
twice=5;
}else if(maths>=21&&maths<22){
console.log('恭喜获得奖品6')
twice=6;
}else if(maths>=22&&maths<30){
console.log('恭喜获得奖品7')
twice=7;
}else if(maths>=30&&maths<100){
console.log('恭喜获得奖品8')
twice=8;
}
maths=80+twice;
light();
}
start()函数也有修改,修改为:
$('.start').click(function() {
if(click&&num>0) {
click = false;
maths = (Math.random() * 100);
maths_f()
} else {
return false;
}
});
其他的自己看着改吧。