zoukankan      html  css  js  c++  java
  • jquery抽奖插件+概率计算

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


    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;
    	}
    };
    
  • 相关阅读:
    Can you answer these queries?(HDU4027+势能线段树)
    Tunnel Warfare(HDU1540+线段树+区间合并)
    Computer(HDU2196+树形dp+树的直径)
    Anniversary party(树上dp+HDU1520)
    2018 Multi-University Training Contest 1-1002 -Balanced Sequence(括号匹配+贪心)
    暑假集训——cf热身赛部分题有感加其题解
    Educational Codeforces Round 47 (Rated for Div. 2) 题解
    计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)
    多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))
    Codeforces Round #494 (Div. 3)
  • 原文地址:https://www.cnblogs.com/blackwood/p/3842695.html
Copyright © 2011-2022 走看看