JavaScript概率游戏抽奖根据cookie判断是否填写资料,从而显示相关的提示;判断元素下标是否在随机生成的数组中,有即让其抽奖

/根据cookie判断是否填写资料,从而显示相关的提示;/
if(.cookie('EventsUserName') != null &&.cookie(‘EventsAccount’) != null && $.cookie(‘EventsTel’) != null) {
infoCont.hide();
joinTip.show();

    $ (".tip_joinButt").hide();
    joinButt.addClass('success');
    $('.receiveButt').addClass('receive')



}
if($.cookie('EventsUserName') == null && $.cookie('EventsAccount') == null && $.cookie('EventsTel') == null) {
    infoCont.show();
    joinTip.hide();
    joinButt.removeClass('success');
    $('.receiveButt').removeClass('receive')
}
 $ (".tip_joinButt").hover (function () {
    $ (this).addClass ("egg01_tip");}, function () {$ (this).removeClass ('egg01_tip'); });//tip:世间万事,皆有规则,请填左侧资料,再来游戏一番~


//点击报名参加
joinButt.on('click', function() {
    if(joinButt.hasClass('success')) {
        infoCont.hide();
        joinTip.show();

    }
})


//弹出提示框
function alertShow(obj) {
    alterTips_bg.show();
    obj.show();
    setTimeout(function() {
        alterTips_bg.hide();
        obj.hide();
    }, 1800); //过1秒未填写资料提示框消失
}

//点击报名参加
tree_btn.one('click', function() {
    if(joinButt.hasClass('success')) {
        //infoCont.hide();
        $ (".egg01_tip").hide();
        $ (".tip_joinButt").hide();
        joinTip.show();
        $('#gameCont').css('cursor','url(images/baozhen_13.ico),auto');
        $(this).addClass('lingqu');
        alertShow(receivetreeTip);
    }
})

$ (".sapling-tip").on('mouseover mouseout',function (e) {
    var _this=$(this);
    if(tree_btn.hasClass('lingqu') && !_this.hasClass('receive')){
        if(e.type=="mouseover"){
            _this.find('.sapling_t').show();                    
        }
        else if(e.type == "mouseout"){
            //alert(12)
            $('.sapling_t').hide();

        }
    }   
});//tip:把绿色还给春天,让红色留给投资!2017一路飘红~~

var prizehtml = "";

function getEle(arr, num) {
    var tempArray = []; //定义一个数组来保存传入的参数数组,可避免直接操作传入的数组
    for(var i in arr) {
        tempArray.push(arr[i]);
    }
    var returnArray = []; //定义一个数组来保存随机抽取的元素
    for(var j = 0; j < num; j++) {
        if(tempArray.length > 0) {
            var arrIndex = Math.floor(Math.random() * tempArray.length); //定义随机抽取的元素下标
            returnArray[j] = tempArray[arrIndex]; //把抽取的元素赋值给返回的数组
            tempArray.splice(arrIndex, 1) //把抽取出来的元素在原数组中删除,避免抽取到重复项
        } else {
            break; //数组中数据抽取完后,退出循环
        }
    }
    return returnArray;
}
var list = [0, 1, 2, 3, 4, 5];
var randList = getEle(list, 2);
var isActive = true;
var userId = $('#userId');
var clickNum = "";


eggButt.on('click', function() {
    //未填写资料点击秒杀按钮进行相关提示
    //开始点抽奖
    //未填写资料点击秒杀按钮进行相关提示        
    if (!joinButt.hasClass('success')) {
        $ (".egg01").hover (function () {
                                $ (this).addClass ("egg01_tip");}, function () {
                                    $ (this).removeClass ('egg01_tip'); });//tip:世间万事,皆有规则,请填左侧资料,再来游戏一番~

      $('html,body').animate({ scrollTop: $('#infoMain').offset().top }, 300);//垂直滚动条值
        alertShow(notjoinTip);


    }
    else {
        var _this = $(this).parent(); // 
        var _thisIndex = $(this).parents('li').index();
        var prizeImg = $('#prize');
        if(tree_btn.hasClass('lingqu')){
            if(!_this.hasClass('receive') && _this.find('.sapling').length <= 0){
                if(isActive){                                   
                    isActive = false; //防止动作未完成之前触发多次点击事件                       
                    $.ajax({
                        type: 'get',
                        url: 'test1.txt',
                        data: {
                            userId: userId.val(),
                            clickNum:clickNum
                        },
                        dataType: 'json',
                        success: function(result) {
                            if(result.status == 'succeed') {
                                if(result.clickNum == 6 || clickNum == 6 ) {
                                    alertShow(tip_repeat02);//alert('一个账户只能玩一次,一次可以植树6次')
                                } 
                                else {
                                    clickNum++;
                                    _this.append('<img class="sapling" src="images/egg01-break.png" />');
                                    /*_this.find('.sapling').animate({
                                        'height': '80',
                                        'margin-left': -62.63 / 2
                                    }, 500);*/
                                    //setTimeout(function(){
                                        _this.css('cursor','default');//cursor小树苗
                                        //_this.find('.sapling').attr('src',"images/egg01-break.png");
                                        _this.find('.sapling').animate({
                                            'height': '255',
                                            'margin-left': -_this.width() / 2
                                        }, 1500);
                                    //},500)




                                    setTimeout(function() {
                                        //$('.sapling_t').hide();//隐藏把绿色还给春天,让红色留给投资!2017一路飘红~~
                                        isActive = true; //恢复按钮的点击状态
                                        _this.addClass('receive');
                                        for(var i = 0; i < randList.length; i++) {
                                            alterTips_bg.show();
                                            successTip.show(); 
                                            if(_thisIndex == randList[0]) { //判断元素下标是否在随机生成的数组中,有即让其抽奖
                                                var random = Math.random();

                                                if(random <= 0.05) {

                                                prizeImg.attr('src', 'images/prizeImg/gold.png');
                                                prizeImg.attr('alt', '足金金蛋吊坠'); 
                                                $(".font").prepend('足金金蛋吊坠'); 

                                                } else if(random > 0.05 && random <= 0.15) {

                                                prizeImg.attr('src', 'images/prizeImg/pillow.png');
                                                prizeImg.attr('alt', '舒适汽车护颈头枕一对'); 
                                                 $(".font").prepend('舒适汽车护颈头枕一对'); 

                                                } else if(random > 0.15 && random <= 0.8) {


                                                prizeImg.attr('src', 'images/prizeImg/mat.png');
                                                prizeImg.attr('alt', '户外野餐垫'); 
                                                 $(".font").prepend('户外野餐垫'); 

                                                } else {

                                                prizeImg.attr('src', 'images/prizeImg/gold_b.png');
                                                prizeImg.attr('alt', '999金币10个'); 
                                                 $(".font").prepend('999金币10个'); 

                                                }
                                                return false;
                                            } else if(_thisIndex == randList[1]) {

                                                prizeImg.attr('src', 'images/prizeImg/book.png');
                                                prizeImg.attr('alt', '黄金');
                                                $(".font").prepend('黄金'); 

                                                return false;
                                            }
                                        }
                                         alertShow(sorry);
                                         successTip.hide(); 
                                        //alert('很遗憾,与大奖擦肩而过!');
                                    }, 1500)

                                }
                            }                               
                        },
                        error: function() {
                            //alert('查询出错')
                        }
                    })
                }
            }
            else {
                if(isActive){
                    alertShow(tip_repeat01);
                    //alert('游戏虽好,切莫贪玩,奖品已领,下期再会~');
                }
            }
        }
        else{
            alertShow(notreceived);//还没领树苗~提示
        }
    }

})




//关闭弹窗
function closeTips() {
    alterTips_bg.hide();
    alertTips.hide();
    $('.egg').show();
    $('.breakegg').hide();  //还原 效果
}

closeButt.on('click', closeTips);  //关闭提示   



$('#knowButt').on('click', function() { //关闭重复领奖弹窗
    closeTips();
    $(this).siblings('.egg').show().siblings('.breakegg').show();
});

$('#receiveButt').on('click', function() { //领取奖品
    closeTips();
    $(this).siblings('.egg').show().siblings('.breakegg').show();
    $('.alterTips').hide();
     alertShow(receiveTip); 
    $('.breakegg').show(); 

})

})

//读取中奖名单内容
.ajax({  
    type: “get”,  
    url: “test1.txt”,  
    dataType: ‘json’,  
    /* data: { type: “selectprizepeople”, Soure: document.title },*/  
    success: function(result) {  
        if(result.status == “succeed”) {
.each(result.obj, function(index, data) {
$(‘#winList’).append(‘

  • ’ + GetString(data.UserName) + ‘

    砸中 ’ + data.Prize + ‘

  • ’);
    })
    carouselUp(‘#carouselUp’);
    } else {
    carouselUp(‘#carouselUp’);
    }
    }
    });

    /把查询出来的中奖名单的用户信息截取字段隐藏/
    function GetString(str) {
    if(str != “”) {
    var string = str.substring(0, 1) + ‘*’ + str.substring(2, str.length)
    return string;
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值