九宫格大转盘抽奖源码-超实用!

九宫格大转盘抽奖源码-超实用!

一、用到的图片

九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!九宫格大转盘抽奖源码-超实用!

二、代码如下,重点是js部分

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery九宫格大转盘抽奖</title>
<style>
#lottery{width:570px;height:510px;margin:0px auto;border:4px solid #ba1809;}
#lottery table{background-color:yellow;}
#lottery table td{position:relative;width:190px;height:170px;text-align:center;color:#333;font-index:-999}
#lottery table td img{display:block;width:190px;height:170px;}
#lottery table td a{width:190px;height:170px;display:block;text-decoration:none;background:url(images/lottery1.jpg) no-repeat top center;}
#lottery table td a:hover{background-image:url(images/lottery2.jpg);}
#lottery table td.active .mask{display:block;}
.mask{
    width:100%;
    height:100%;
    position:absolute;
    left:0;
    top:0;
    background:url(images/mask.png) no-repeat;
    display:none;
}
</style>
</head>
<body class="keBody">
<!--效果html开始-->
<div id="lottery">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td class="lottery-unit lottery-unit-0"><img src="images/gift0.jpg"><div class="mask"></div></td>
            <td class="lottery-unit lottery-unit-1"><img src="images/gift1.jpg"><div class="mask"></div></td>
            <td class="lottery-unit lottery-unit-2"><img src="images/gift2.jpg"><div class="mask"></div></td>
        </tr>
        <tr>
            <td class="lottery-unit lottery-unit-7"><img src="images/gift7.jpg"><div class="mask"></div></td>
            <td><a href="#"></a></td>
            <td class="lottery-unit lottery-unit-3"><img src="images/gift3.jpg"><div class="mask"></div></td>
        </tr>
        <tr>
            <td class="lottery-unit lottery-unit-6"><img src="images/gift6.jpg"><div class="mask"></div></td>
            <td class="lottery-unit lottery-unit-5"><img src="images/gift5.jpg"><div class="mask"></div></td>
            <td class="lottery-unit lottery-unit-4"><img src="images/gift4.jpg"><div class="mask"></div></td>
        </tr>
    </table>
</div>

<script src="http://code.jquery.com/jquery-latest.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,    //中奖位置
    init:function(id){
        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(){
        var index = this.index;
        var count = this.count;
        var lottery = this.obj;
        $(lottery).find(".lottery-unit-"+index).removeClass("active");
        index += 1;
        if (index>count-1) {
            index = 0;
        };
        $(lottery).find(".lottery-unit-"+index).addClass("active");
        this.index=index;
        return false;
    },
    stop:function(index){
        this.prize=index;
        return false;
    }
};

function roll(){
    lottery.times += 1;
    lottery.roll();//转动过程调用的是lottery的roll方法,这里是第一次调用初始化
    if (lottery.times > lottery.cycle+10 && lottery.prize==lottery.index) {
        clearTimeout(lottery.timer);
        lottery.prize=-1;
        lottery.times=0;
        click=false;
    }else{
        if (lottery.times<lottery.cycle) {
            lottery.speed -= 10;
        }else if(lottery.times==lottery.cycle) {
            var index = Math.random()*(lottery.count)|0;
            lottery.prize = index;        
        }else{
            if (lottery.times > lottery.cycle+10 && ((lottery.prize==0 && lottery.index==7) || lottery.prize==lottery.index+1)) {
                lottery.speed += 110;
            }else{
                lottery.speed += 20;
            }
        }
        if (lottery.speed<40) {
            lottery.speed=40;
        };
        //console.log(lottery.times+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize);
        lottery.timer = setTimeout(roll,lottery.speed);//循环调用
    }
    return false;
}

var click=false;

window.onload=function(){
    lottery.init('lottery');
    $("#lottery a").click(function(){
        if (click) {//click控制一次抽奖过程中不能重复点击抽奖按钮,后面的点击不响应
            return false;
        }else{
            lottery.speed=100;
            roll();    //转圈过程不响应click事件,会将click置为false
            click=true; //一次抽奖完成后,设置click为true,可继续抽奖
            return false;
        }
    });
};
</script>
<!--效果html结束-->

</body>
</html>

三、注意事项

1、抽奖过程说明

上面只是前端展示的效果。中奖物品通过一个随机数生成。

var index = Math.random()*(lottery.count)|0;

真正开发中中奖物品是通过向后端接口发送请求返回的。

$("#lottery a").click(function(){
             var islogin=checkLogin();
             if(islogin){//已登录用户才能去抽奖
                 if (click) {
                    return false;
                }else{
                    //向后端接口发请求返回中奖结果
                    var geturl="http://xxxxxx?username="+username+"&token="+token;
                     $.ajax({
                      url:geturl,
                       type:"GET",
                       dataType:"json",
                      async:false,
                    success:function(data){
                           if(data.errorcode==0){
                            var rewardid=data["message"]["rewardid"];
                            var cardno=data["message"]["rewardCardNo"];
                              var passno=data["message"]["rewardCardPass"];
                              var prize=-1;
                               var content="";
                              if(rewardid=="iphone6"){
                             lottery.prize=0;
                             prize=0;
                            content="一部iphone6手机";
                            $("#content1").html(content);
                               }else if(rewardid=="PPTVKING"){
                               lottery.prize=1;
                            prize=1;
                              content="一部PPTV KING7s  3D影音手机";
                             $("#content1").html(content);
                               /*...    */
                             }else if(rewardid=="legao"){
                            lottery.prize=5;
                            prize=5;
                            content="一份乐高的玩具";
                               $("#content1").html(content);
                             }
                            
                            lottery.speed=100;
                            roll();
                            click=true;    
                            return false;
                         }else{
                            /*错误处理*/
                              if(data.errorcode==3){
                            $("#novip").show();
                              }else{
                            $("#notime").show();
                             }
                          }
                      }/*function结束*/
                 });/*ajax结束*/
            }/*else结束*/
           }
});

2、兼容性说明

.mask开始如下,用的是rgba,但是IE8不兼容,改为使用png图片background:url(images/mask.png) no-repeat;

.mask {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background-color: rgba(252,211,4,0.5);
    display: none
}

 

本文转载自http://www.cnblogs.com/starof/p/4933907.html,原文链接: ,本文观点不代表125jz立场。

(0)
江山如画的头像江山如画管理团队
上一篇 2021年1月25日 上午9:05
下一篇 2021年1月30日 上午11:20

99%的人还看了以下文章

  • 4个最常用的css3动画库-300多款不同类型的CSS3动画源码

    一、animates.css animate.css是来自dropbox的工程师Daniel Eden开发的一款CSS3的动画效果小类库。包含了60多款不同类型的CSS3动画,包括:晃动,闪动,各种淡出淡出效果,如果你想快速的整合各种CSS3动画特效的话,使用它即可方便的实现。 查看演示: https://daneden.github.io/animate.…

    2021年1月25日 源码下载
    1.9K0
  • 值得收藏的日历插件-适用订退房,订往返车票,超出的时间段为灰状不可选

    分享一个值得收藏的日历插件-适用订退房,订往返车票等。 可以根据需求进行扩充,挺好的一款日历插件,界面简洁美观,值得拥有和收藏。 功能: 像12306火车订票一样,只能在一个时间段内选择,超出的时间段为灰状不可选; 选择前面日期的时候,后面的日期会跟着跳动,并比前者晚一天;选择后者的日期时候,如果后者早于或等于前者,那么将后者至于比前者晚一天的那个日期。 并…

    2019年11月1日
    1.7K0
  • 网页中制作精美饼状图源码

    本文使用HTML5 Canvas和jQuery轻松制作精美的动态饼状图,当鼠标点击某个区域时,会动态分享,并高亮显示。饼状图根据表格中指定数据生成,修改使用都非常方便,值得收藏。

    2022年7月14日
    7620
  • 免费蹭网!破解WiFi密码的Python程序源码分享

    文章主要介绍了如何利用Python探测附近WIFI基于python脚本实现wifi密码的暴力破解从而实现免费蹭网,不建议大家做任何破坏性的操作和任何不当的行为!

    2023年1月29日
    3.4K0
  • 酷!一款绿色风格的日期时间选择器源码

    bootstrap-material-datetimepicker是一款基于Bootstrap的Material Design风格日期时间选择器插件。 通过这个插件可以快速的构建扁平风格的日期选择器。它还带有非常炫酷的浮动标签等Material效果,非常的时尚。

    2020年4月4日
    2.3K0
  • 实现按拼音或首字母检索城市的JS源码

    项目开发中,需要选择城市,如汽车/火车/飞机的出发站,到达站等,因为站点或城市太多,需要实现用户输入拼音或首字母就能自动检索。 实现这个功能还是有难度的,125网页设计站长给大家分享实现代码,只用引用这个js就可以实现。 实现按拼音/首字母检索城市的js源码: See the Pen 实现按拼音首字母检索城市的js插件 by 江山如画 (@jgyzhr)on…

    2019年10月31日
    3.5K0

发表回复

登录后才能评论