博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery抽奖插件+概率计算
阅读量:6891 次
发布时间:2019-06-27

本文共 4375 字,大约阅读时间需要 14 分钟。

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

121104342541373.jpg

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

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

你可能感兴趣的文章
学习笔记-linux文件特殊基本权限基础
查看>>
Spring quartz的Job中如何获取Spring上下文
查看>>
FTP服务
查看>>
C#判断IP是否是指定范围内的IP
查看>>
TEC-005-cifs-Host is down
查看>>
saltstack模块之pkg相关模块
查看>>
linux查看内核版本号
查看>>
SVN合代码时遇到的问题
查看>>
tuna.tsinghua yum repo
查看>>
ext store remove old datas load new datas优化
查看>>
【Jetty Server 开发系列之一】搭建Jetty Server环境&&Http客户端实现交互
查看>>
【COCOS2D-HTML5 开发之三】示例项目附源码及运行的GIF效果图
查看>>
mysql5.6的安装(rpm)
查看>>
Gamebryo实例学习之八InputDemo
查看>>
关于CSDN2013博客之星的一些看法
查看>>
"安全删除硬件并弹出媒体"的列表中出现内置硬盘的解决办法.
查看>>
LINUX中JDK环境变量配置
查看>>
linux 切换用户之后变成-bash-3.2$的解决方法
查看>>
我的友情链接
查看>>
使用list
查看>>