寫一個(gè)九格老虎機(jī)

去年開(kāi)始,抽獎(jiǎng)活動(dòng)的形式發(fā)生了一些變化。圓形的輪盤都已告別歷史舞臺(tái),九格輪盤成為了主流。調(diào)研了一早上,這里出一個(gè)簡(jiǎn)化版。

首先用css畫(huà)九個(gè)格子,如下

Paste_Image.png

這個(gè)東西的邏輯是,點(diǎn)擊開(kāi)始后,獲得結(jié)束位置,隨機(jī)分配一些圈數(shù)。
對(duì)于非連續(xù)的跳躍,使用setTimeout來(lái)控制旋轉(zhuǎn)速度。

function roulette() {
       
        this._startbox = 0;         //起點(diǎn)位置
        this._endbox = 1;          //終點(diǎn)位置
        this._jumpnum = 24;        //路徑中所經(jīng)歷的格子數(shù)
        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); //返回當(dāng)前這一步的耗時(shí)
            self.showbox(jumpindex); //對(duì)顯示進(jìn)行控制
            jumpindex++;
            if (jumpindex >= jumpmax) {
                clearTimeout(timer);
                self._startbox = self._endbox;    //終點(diǎn)作為下次起點(diǎn)
                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;
        }
    }

重點(diǎn)是做出加速減速的感覺(jué)。其他沒(méi)啥了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這種事做一次就夠了別逼自己再犯賤,以后不會(huì)再這樣了
    季莎閱讀 203評(píng)論 0 0
  • 斷掉千絲向一路 舍棄悲怨近光明 離開(kāi)繁雜歸簡(jiǎn)樸 大道至簡(jiǎn)是真途 精神富裕無(wú)多物 吃飽穿暖亦知足 人生匆匆百十年 少...
    神于天圣于地閱讀 276評(píng)論 0 2
  • 作為一個(gè)每逢考試不是坐第一排就是坐最后一排的人,從不買彩票,及時(shí)買了彩票也不會(huì)中獎(jiǎng),所有的幸運(yùn)均和我無(wú)關(guān)時(shí),我對(duì)很...
    臻靜閱讀 145評(píng)論 0 0
  • 本系列讀書(shū)筆記是小荻老師發(fā)起的《刻意練習(xí)》讀書(shū)活動(dòng)的產(chǎn)出。以后一個(gè)月的時(shí)間里,都會(huì)以「閱讀」+「思考」+「想法」的...
    Ingeliu閱讀 241評(píng)論 0 0
  • 最近結(jié)識(shí)了一位朋友,看到他,我竟有些懷疑自己的生活狀態(tài)與方式。 在此之前,我從未覺(jué)得自己活的舒適,與我身邊的朋友來(lái)...
    然谷中醫(yī)閱讀 303評(píng)論 0 0

友情鏈接更多精彩內(nèi)容