jquery抽奖插件+概率计算

写了一个抽奖的jquery插件和计算概率的方法, 结合起来就是一个简单的概率抽奖, 不过实际项目中基本不会把抽奖概率的计算放在前端处理~。

121104342541373.jpg
demo

lottery.jquery.js

$.fn.extend({
    lottery: function(conf) { 
        var def = {
            lotIndex: 0,            // 抽中的索引
            item: "li",
            onClass: "on",
            speedStart: 50,         // 初始速度
            speedEnd: 400,          // 结束速度
            speedType: "",          // 默认匀速 可选 change: 减速
            overTime: 5000,         // 抽奖时长(最短)
            overCallback: function() {}     // 抽奖结束后的回调函数
        };
        
        if (typeof conf.lotIndex === "undefined") {
            return
        }
        
        def = $.extend({}, def, conf);
        
        var $lotteryList = $(this),
            lotteryControl = {};
        
        lotteryControl = {
            $el: $lotteryList,
            item: def.item,
            itemLen: 0,
            index: 0,
            speedType: def.speedType,
            speedStart: def.speedStart,
            speed: def.speedStart,
            speedEnd: def.speedEnd,
            a: 0,                             // 加速度
            nowTime: 0,                     // 抽奖已进行时间
            overFlag: false,                // 抽奖是否结束
            onClass: def.onClass,
            lotIndex: def.lotIndex,
            overTime: def.overTime,
            overCallback: def.overCallback,
            init: function() {
                this.$items = this.$el.find(this.item);
                this.itemLen = this.$items.length;
                this.a = (this.speedEnd - this.speed) / this.overTime;

                if (this.lotIndex >= this.itemLen) {
                    this.error();
                } else {

                    this.start();
                }
            },
            start: function() {
                var self = this;

                this.play();
                this.setStop();

                switch (this.speedType) {
                    case "change":
                        this.changeSpeed();
                        break;
                }
            },
            play: function() {
                var $items = this.$items;

                $items.eq(this.index - 1).removeClass(this.onClass);
                $items.eq(this.index).addClass(this.onClass);

                if (this.overFlag && this.index === this.lotIndex) {
                    this.stop();
                } else {
                    this.next();
                }
            },
            next: function() {
                var self = this;

                this.index++;
                this.index = this.index === this.itemLen ? 0 : this.index;

                setTimeout(function() {
                    self.play();
                }, this.speed);

            },

            changeSpeed: function() {
                var self = this;

                setTimeout(function() {
                    self.nowTime += self.speed;

                    if (!self.overFlag) {
                        self.speed = self.speedStart + self.a * self.nowTime;

                        self.changeSpeed();
                    }
                }, this.speed);

            },
            setStop: function() {
                var self = this;

                setTimeout(function() {
                    self.overFlag = true;
                }, this.overTime);
            },
            stop: function() {
                this.overCallback();
            },
            error: function() {
                console.log("error.......");
            }
        };
        
        lotteryControl.init( );
        
        return this;
    }
});
            

概率计算

function Probability(conf) {
    this.probArr = conf || [];
    this.range = [],
    this.len = this.probArr.length;
    if (this.len > 0) { 
        this.init();
    }
}
Probability.prototype = {
    init: function() {
        this.setRange();
    },
    get: function() { 
        var len = this.len,
            range = this.range,
            last,
            randNum, 
            i = 0; 
        if (len === 0) {
            return;
        } else if(len === 1) {
            return 0;
        } 
        last = range[len -1];
        randNum = Math.floor(last* Math.random()); 
        for (; i < len; i++) { 
            if (randNum < range[i]) {
                break;
            }
        } 
        return i;
    }, 
    setRange: function() {
        var range = [],
            probArr = this.probArr,
            i = 0,
            len = probArr.length; 
        for(; i<len; i++) {
            var now = probArr[i],
                last = range[i-1] || 0; 
            range.push(now+last);
        }  
        this.range = range;
    }
};

转载于:https://www.cnblogs.com/blackwood/p/3842695.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值