写一个九格老虎机

去年开始,抽奖活动的形式发生了一些变化。圆形的轮盘都已告别历史舞台,九格轮盘成为了主流。调研了一早上,这里出一个简化版。

首先用css画九个格子,如下

Paste_Image.png

这个东西的逻辑是,点击开始后,获得结束位置,随机分配一些圈数。
对于非连续的跳跃,使用setTimeout来控制旋转速度。

function roulette() {
       
        this._startbox = 0;         //起点位置
        this._endbox = 1;          //终点位置
        this._jumpnum = 24;        //路径中所经历的格子数
        this._self = this;
       
}
roulette.prototype.run = function() {
        var self = this._self,
            time = 500,
            jumpmax = this._jumpnum,
            jumpindex = 0,
            timer = null;
        function timerdo() {
            time = self.changeshowlist(jumpindex); //返回当前这一步的耗时
            self.showbox(jumpindex); //对显示进行控制
            jumpindex++;
            if (jumpindex >= jumpmax) {
                clearTimeout(timer);
                self._startbox = self._endbox;    //终点作为下次起点
                setTimeout(function() { console.log('finish'); }, 200);
            }
            else {
                timer = setTimeout(timerdo, time);
            }
        }
        timerdo();
    }
roulette.prototype.showbox = function(index) {
        var myIndex = ( (this._startbox + index) %  $('.blockContainer').length ) ;
        $('.blockContainer').css('backgroundColor', 'red');
        $('#'+myIndex).css('backgroundColor', 'black');
}

    //每次改变需要显示的box,返回速度
    roulette.prototype.changeshowlist = function(jumpindex) {
        var i,
            jumpmax = this._jumpnum;
        switch (jumpindex) {
            case 0:
                
                return 400;
            case 1:
                
                return 350;
            case 2:
                
                return 300;
            case 3:
                
                return 200;
            case jumpmax - 1:
                
                return 800;
            case jumpmax - 2:
                
                return 700;
            case jumpmax - 3:
               
                return 600;
            case jumpmax - 4:
                
                return 400;
            case jumpmax - 5:
               
                return 300;
            case jumpmax - 6:
                
                return 200;
            case jumpmax - 7:
                
                return 100;
            case jumpmax - 8:
                
                return 50;
            default:
               
                return 30;
        }
    }

重点是做出加速减速的感觉。其他没啥了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这种事做一次就够了别逼自己再犯贱,以后不会再这样了
    季莎阅读 1,254评论 0 0
  • 断掉千丝向一路 舍弃悲怨近光明 离开繁杂归简朴 大道至简是真途 精神富裕无多物 吃饱穿暖亦知足 人生匆匆百十年 少...
    神于天圣于地阅读 1,722评论 0 2
  • 作为一个每逢考试不是坐第一排就是坐最后一排的人,从不买彩票,及时买了彩票也不会中奖,所有的幸运均和我无关时,我对很...
    臻静阅读 897评论 0 0
  • 本系列读书笔记是小荻老师发起的《刻意练习》读书活动的产出。以后一个月的时间里,都会以「阅读」+「思考」+「想法」的...
    Ingeliu阅读 1,462评论 0 0
  • 最近结识了一位朋友,看到他,我竟有些怀疑自己的生活状态与方式。 在此之前,我从未觉得自己活的舒适,与我身边的朋友来...
    然谷中医阅读 1,807评论 0 0