html抽奖效果制作图

先上代码:
css:
布局根据实际情况来

html:

             <ul class="lot-ul">
                <li class="index-0"><p class="lp-font">1</p>   </li>
                <li class="index-1"><p class="lp-font">2</p></li>
                            <li class="index-2"><p class="lp-font">3</p></li>

                <li class="index-7"><p class="lp-font">8</p></li>
                <li class="lot-btn"><p class="lot-font">抽奖</p></li>
                <li class="index-3"><p class="lp-font">4</p></li>

                <li class="index-6"><p class="lp-font">5</p></li>
                <li class="index-5"><p class="lp-font">4</p></li>
                <li class="index-4"><p class="lp-font">3</p></li>
             </ul>

js:

  function MyRoll(){
   selfRoll = this;

this.desc = null;
this.point = 8;//转盘的经过点总数 
this.$lotBtn = $(".lot-btn");
this.$lotFirst = $(".index-0");
this.isRunning = false;
this.paramRoll = {
    moveTime: null,//初始时间

    moveTimeAdd: null,//加速度
    round: null,//转的圈数
    lastPos: null,//随机停止位置

    curRound: null,//初始化当前圈数
    counter: null//计数器
};

 }
    MyRoll.prototype = {
constructor: MyRoll,
BeginLot: function(){
    this.$lotBtn.on("click",function(){
        if(!isLogin()){return false;}
        if(!selfRoll.isRunning){
            selfRoll.isRunning = true;      //重置下抽奖状态
            selfRoll.sendRollAjax.call(this);    //发送ajax请求
        }
   });
},
sendRollAjax: function(){
        $.ajax({
            url: "/service/hdplatform/"+hdIndex+"/doRoll",
            type: "POST",
            dataType:"json",
            maskId: this,
            data: { lotteryId: _lotteryId},
            success:function(rsp){
                init();//抽奖次数
                if( rsp.result ){
                    selfRoll.desc = rsp.desc;
                    selfRoll.beforeRoll();
                }
                else{
                    selfRoll.isRunning = false;
                    dialog.showMsgBox(rsp.desc);
                }
            }
        });

 },
 beforeRoll: function( ){
    $(".index-0").addClass("lot-position");
    this.paramRoll.moveTime = 500;//初始时间
        
    this.paramRoll.moveTimeAdd = -40;//加速度
    this.paramRoll.round = 3;//转的圈数
    this.paramRoll.lastPos = Math.floor(Math.random()*7);

    this.paramRoll.curRound = 1;//初始化当前圈数
    this.paramRoll.counter = 0;//计数器
    setTimeout( this.rolling, this.paramRoll.moveTime );
             },
            rolling: function(){
    selfRoll.paramRoll.moveTime += selfRoll.paramRoll.moveTimeAdd;
    selfRoll.paramRoll.counter = 
            (selfRoll.paramRoll.counter+1) % selfRoll.point;

    selfRoll.nextState( selfRoll.paramRoll.counter );

    if( selfRoll.paramRoll.counter == selfRoll.paramRoll.lastPos ){
        if( selfRoll.paramRoll.curRound == selfRoll.paramRoll.round ){
            return selfRoll.finishHandler( selfRoll.paramRoll.lastPos , selfRoll.myDialog );
        }
        selfRoll.paramRoll.curRound = selfRoll.paramRoll.curRound+1;
    }

    setTimeout( selfRoll.rolling, selfRoll.paramRoll.moveTime );
        },
      nextState: function( _counter ){
   //对注释代码的优化---通过增加8来避免为0情况的特殊处理
   var middle = _counter+selfRoll.point;
   $(".index-"+ (middle % selfRoll.point) ).addClass("lot-position");
   $(".index-"+ Number(middle-1) % selfRoll.point ).removeClass("lot-position");

   // if( _counter != 0 ){
   //     $(".index-"+_counter).addClass("lot-position");
   //     $(".index-"+Number(_counter-1) ).removeClass("lot-position");
   // }
   // else{
   //     $(".index-0").addClass("lot-position");
   //     $(".index-7").removeClass("lot-position");
   // }
      },
        finishHandler: function(_lastPos , _myCallBack){
    $(".index-"+_lastPos).removeClass("lot-position");
    this.isRunning = false;

    _myCallBack();//执行抽奖完成后的回调函数
     },
    myDialog: function(){
    dialog.showMsgBox(selfRoll.desc);
     }
    }
   new MyRoll().BeginLot();

将抽奖效果分为各个阶段:
beginLot: 点击按钮的时候,如果不是正在抽奖 !isRunning为true,即允许抽奖;否则,允许抽奖,并且设置其为isRunning为true.
sendRollAjax: 设置发送ajax请求,当知道请求结果后,开始转动抽奖盘。
beforeRoll: 转动转盘的时候需要初始化第一个位置样式和转动的速度
rolling:递归函数调用,每一次设置下一个位置的样式,并判断该位置是不是到达终点了,如果不是,则调用自己
nextState: 下一个位置的样式设置
finishHandler: 执行完抽奖后,设置初始样式,并且设置为可以抽奖,isRunning为false;
myDialog: 最后抽奖的结果弹出个窗口。
注意:此抽奖只是模拟抽奖转动,最终结果以弹出的信息框为准。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,224评论 19 139
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,776评论 0 15
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,703评论 1 92
  • 《裕语言》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 10...
    叶染柒丶阅读 28,591评论 5 20
  • 「上班·谈写作」有奖征集:老板,到我办公室来一下 我是你的领导,有一阵子你背地里称我为老大,之后不只你,我的其他下...
    大力稳重阅读 6,982评论 31 80

友情链接更多精彩内容