九宫格子转盘抽奖代码

1、代码如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>九宫格子转盘抽奖代码</title>

<style type="text/css">
#lottery{width:574px;height:584px;margin:20px auto 0;background:url(images/bg.jpg) no-repeat;padding:50px 55px;}
#lottery table td{width:142px;height:142px;text-align:center;vertical-align:middle;font-size:24px;color:#333;font-index:-999}
#lottery table td a{width:284px;height:284px;line-height:150px;display:block;text-decoration:none;}
#lottery table td.active{background:#fff;opacity:0.7;filter:alpha(opacity=70);}
</style>

</head>
<body>
<!-- 代码 开始 -->
<div id="lottery">
    
</div>

<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
var lottery = {
    index:-1,    //当前转动到哪个位置,起点位置
    count:0,    //总共有多少个位置
    timer:0,    //setTimeout的ID,用clearTimeout清除
    speed:20,    //初始转动速度
    times:0,    //转动次数
    cycle:50,    //转动基本次数:即至少需要转动多少次再进入抽奖环节
    prize:-1,    //中奖位置
    click:false,
    growHtml:function(num){//生成html结构 num 总共多少个奖项(支持8,10,12)
        var a = [];
        a.push('<table border="0" cellpadding="0" cellspacing="0">');
        if(num == 12){
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-1"><img src="images/1.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-2"><img src="images/2.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-3"><img src="images/3.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-4"><img src="images/4.png"></td>');
            a.push('    </tr>');
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-12"><img src="images/12.png"></td>');
            a.push('        <td colspan="2" rowspan="2"><a href="javascript:;" class="startGzupan"></a></td>');
            a.push('        <td class="lottery-unit lottery-unit-5"><img src="images/5.png"></td>');
            a.push('    </tr>');
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-11"><img src="images/11.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-6"><img src="images/6.png"></td>');
            a.push('    </tr>');
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-10"><img src="images/10.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-9"><img src="images/9.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-8"><img src="images/8.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-7"><img src="images/7.png"></td>');
            a.push('    </tr>');
        }else if(num == 8){
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-1"><img src="images/1.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-2"><img src="images/2.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-3"><img src="images/3.png"></td>');
            a.push('    </tr>');
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-8"><img src="images/8.png"></td>');
            a.push('        <td><a href="javascript:;" class="startGzupan"></a></td>');
            a.push('        <td class="lottery-unit lottery-unit-4"><img src="images/4.png"></td>');
            a.push('    </tr>');
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-7"><img src="images/7.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-6"><img src="images/6.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-5"><img src="images/5.png"></td>');
            a.push('    </tr>');
        }else if(num == 10){
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-1"><img src="images/1.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-2"><img src="images/2.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-3"><img src="images/3.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-4"><img src="images/4.png"></td>');
            a.push('    </tr>');
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-10"><img src="images/10.png"></td>');
            a.push('        <td colspan="2" rowspan="1"><a href="javascript:;" class="startGzupan"></a></td>');
            a.push('        <td class="lottery-unit lottery-unit-5"><img src="images/5.png"></td>');
            a.push('    </tr>');
            a.push('    <tr>');
            a.push('        <td class="lottery-unit lottery-unit-9"><img src="images/9.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-8"><img src="images/8.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-7"><img src="images/7.png"></td>');
            a.push('        <td class="lottery-unit lottery-unit-6"><img src="images/6.png"></td>');
            a.push('    </tr>');
        }
        a.push('</table>');
        return a.join('');
    },
    init:function(id,priNum){
        $('#' + id).html(lottery.growHtml(priNum));//生成机构
        if ($("#" + id).find(".lottery-unit").length > 0) {
            $lottery = $("#" + id);
            $units = $lottery.find(".lottery-unit");
            this.obj = $lottery;
            this.count = $units.length;
            $lottery.find(".lottery-unit-" + this.index).addClass("active");
        };
    },
    roll:function(index){
        var index = this.index;
        var count = this.count;
        var lottery = this.obj;
        $(lottery).find(".lottery-unit-" + index).removeClass("active");
        index += 1;
        if (index > count) {
            index = 0;
        };
        $(lottery).find(".lottery-unit-" + index).addClass("active");
        this.index = index;
        return false;
    },
    stop:function(index){
        this.prize = index;
        return false;
    },
    roll_prize:function(prizeNum,callback){
        this.times += 1;
        this.roll();
        if (this.times > this.cycle + 10 && this.prize == this.index) {
            clearTimeout(this.timer);
            this.prize = -1;
            this.times = 0;
            this.click = false;
            if(callback){//添加回调
                callback();
            }
        }else{
            if (this.times < this.cycle) {
                this.speed -= 10;
            }else if(this.times == this.cycle) {
                var index = prizeNum;
                this.prize = index;        
            }else{
                if (this.times > this.cycle + 10 && ((this.prize == 0 && this.index == 7) || this.prize == this.index + 1)) {
                    this.speed += 110;
                }else{
                    this.speed += 20;
                }
            }
            if (this.speed < 40) {
                this.speed = 40;
            };
            this.timer = setTimeout(function(){
                lottery.roll_prize(prizeNum,callback);
            },this.speed);
        }
        return false;
    },
    start:function(num,callback){
        if (this.click) {
            return false;
        }else{
            this.speed = 100;
            this.roll_prize(num,callback);
            this.click = true;
            return false;
        }
    }
};


//
$(function(){
    lottery.init('lottery',12);//初始化
    $(".startGzupan").click(function(){
        lottery.start(11,function(){
            alert('中奖了!');
        });
    });
});
</script>

</body>
</html>

 附件下载地址

posted @ 2015-12-31 16:14  史洲宇  阅读(999)  评论(0编辑  收藏  举报