1. 转圈的顺序(顺时针或者逆时针);css
2. 转圈的速率(从慢到快再到慢);前端
3. 位置的问题(下一次抽奖的起始位置是上一次抽奖的结束位置);css3
4. 转圈的圈数或者移动的次数。dom
1. 抽奖的过程其实就是经过不断的改变 dom(一般为 li
)的索引值,来达到移动的效果(好比 8 个奖项,索引值的变化以下 0 -> 1, 1 -> 2, ... , 6 -> 7 , 7 -> 0),因此 dom 的排版(绝对定位)就显得很重要了;函数
2. 对于移动的速度其实就是 dom 切换的快慢,在 js 中,咱们能够经过定时器 setTimeout 的执行时间来控制移动的速度;动画
3. 移动速度的增减;this
4. 何时移动结束。spa
1. 首先经过构造函数实例化,而且在实例化时传入相关参数(通常都为 object)(好比 var lottery = new Lottery(opts)),来实现该组件的初始化;指针
2. 组件初始化后,还须要有个方法来执行抽奖的过程(好比 lottery.start());code
3. 固然咱们还须要告诉组件中的是什么奖励(通常都是接口调用返回给前端),即中奖的位置在哪里,至关于奖励的索引(好比 lottery.prize = 1)。
'use strict'; module.exports = function Lottery(opts) { this.index = opts.index || 0; this.speed = opts.speed || 60; this.maxSpeed = opts.maxSpeed || 40; this.minSpeed = opts.minSpeed || 250; this.base = opts.base || 3; this.totals = opts.totals || 12; this.length = opts.length || 8; this.prize = -1; // 中奖位置,须要外部来更新 var self = this; var speed = self.speed; var timer = null; // 定时器 ID var counts = 0; // 移动次数计数 // 在移动的过程当中,其实就是索引值的改变,好比 0 -> 1, 1 -> 2, ... , 6 -> 7 , 7 -> 0 var move = function() { var index = self.index; var len = self.length; var before = self.index; // 当前 index += 1; // 下一个 // 当索引到最后一个时,下一个索引就是第一个 if (index > len - 1) { index = 0; } opts.moveTo && opts.moveTo(before, index); self.index = index; }; // 外部调用抽奖方法 this.start = function() { move(); counts += 1; if (counts > this.totals && this.prize === this.index) { clearTimeout(timer); counts = 0; speed = this.speed; this.prize = -1; opts.callback && opts.callback(); } else { if (counts < this.base) { speed -= 10; } else { if (counts > this.totals) { speed += 60; } else { speed += 30; } } speed = speed < this.maxSpeed ? this.maxSpeed : (speed > this.minSpeed ? this.minSpeed : speed); timer = setTimeout(function() { self.start(); }, speed); } }; };
1. 不限九宫格排列的抽奖;
2. 能够知足圆盘式指针旋转的抽奖。