diff --git a/bower.json b/bower.json index aa55fab..4d976b1 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "jQuery-SlotMachine", "description": "A simple jQuery plugin to make slot machine animation effect", - "version": "2.2.0", + "version": "2.3.0", "keywords": [ "slots", "gambling", diff --git a/dist/jquery.slotmachine.js b/dist/jquery.slotmachine.js index 71a64b6..fa0f3aa 100644 --- a/dist/jquery.slotmachine.js +++ b/dist/jquery.slotmachine.js @@ -1,632 +1,747 @@ -/*! SlotMachine - v2.2.0 - 2015-11-05 +/*! SlotMachine - v2.3.0 - 2015-12-22 * https://github.com/josex2r/jQuery-SlotMachine * Copyright (c) 2015 Jose Luis Represa; Licensed MIT */ -(function($, window, document, undefined) { - - var pluginName = "slotMachine", - defaults = { - active: 0, //Active element [Number] - delay: 200, //Animation time [Number] - auto: false, //Repeat delay [false||Number] - spins: 5, //Number of spins when auto [Number] - randomize: null, //Randomize function, must return a number with the selected position - complete: null, //Callback function(result) - stopHidden: true, //Stops animations if the element isn´t visible on the screen - direction: 'up' //Animation direction ['up'||'down'] - }; - - var FX_FAST = 'slotMachineBlurFast', - FX_NORMAL = 'slotMachineBlurMedium', - FX_SLOW = 'slotMachineBlurSlow', - FX_GRADIENT = 'slotMachineGradient', - FX_STOP = FX_GRADIENT; - - //Set required styles, filters and masks - $(document).ready(function() { - - var slotMachineBlurFilterFastString = '' + - '' + - '' + - '' + - '#slotMachineBlurFilterFast'; - - var slotMachineBlurFilterMediumString = '' + - '' + - '' + - '' + - '#slotMachineBlurFilterMedium'; - - var slotMachineBlurFilterSlowString = '' + - '' + - '' + - '' + - '#slotMachineBlurFilterSlow'; - - var slotMachineFadeMaskString = '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '#slotMachineFadeMask'; - - //CSS classes - $('body').append(''); - - }); - - //Required easing functions - if (typeof $.easing.easeOutBounce !== 'function') { - //From jQuery easing, extend jQuery animations functions - $.extend($.easing, { - easeOutBounce: function(x, t, b, c, d) { - if ((t /= d) < (1 / 2.75)) { - return c * (7.5625 * t * t) + b; - } else if (t < (2 / 2.75)) { - return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b; - } else if (t < (2.5 / 2.75)) { - return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b; - } else { - return c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b; - } - }, +'use strict'; + +var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +/* + * jQuery Slot Machine v2.1.0 + * https:// github.com/josex2r/jQuery-SlotMachine + * + * Copyright 2014 Jose Luis Represa + * Released under the MIT license + */ +(function init($, window, document, undefined) { + + var pluginName = 'slotMachine', + defaults = { + active: 0, // Active element [Number] + delay: 200, // Animation time [Number] + auto: false, // Repeat delay [false||Number] + spins: 5, // Number of spins when auto [Number] + randomize: null, // Randomize function, must return a number with the selected position + complete: null, // Callback function(result) + stopHidden: true, // Stops animations if the element isn´t visible on the screen + direction: 'up' // Animation direction ['up'||'down'] + }, + FX_FAST = 'slotMachineBlurFast', + FX_NORMAL = 'slotMachineBlurMedium', + FX_SLOW = 'slotMachineBlurSlow', + FX_GRADIENT = 'slotMachineGradient', + FX_STOP = FX_GRADIENT; + + // Set required styles, filters and masks + $(document).ready(function documentReady() { + + var slotMachineBlurFilterFastString = '' + '' + '' + '' + '#slotMachineBlurFilterFast'; + + var slotMachineBlurFilterMediumString = '' + '' + '' + '' + '#slotMachineBlurFilterMedium'; + + var slotMachineBlurFilterSlowString = '' + '' + '' + '' + '#slotMachineBlurFilterSlow'; + + var slotMachineFadeMaskString = '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '#slotMachineFadeMask'; + + // CSS classes + $('body').append(''); }); - } - - - function Timer(fn, delay) { - var startTime, - self = this, - timer, - _fn = fn, - _args = arguments, - _delay = delay; - - this.running = false; - - this.onpause = function() {}; - this.onresume = function() {}; - - this.cancel = function() { - this.running = false; - clearTimeout(timer); - }; - - this.pause = function() { - if (this.running) { - delay -= new Date().getTime() - startTime; - this.cancel(); - this.onpause(); - } - }; - - this.resume = function() { - if (!this.running) { - this.running = true; - startTime = new Date().getTime(); - - timer = setTimeout(function() { - _fn.apply(self, Array.prototype.slice.call(_args, 2, _args.length)); //Execute function with initial arguments, removing (fn & delay) - }, delay); - this.onresume(); - } - }; - - this.reset = function() { - this.cancel(); - this.running = true; - delay = _delay; - timer = setTimeout(function() { - _fn.apply(self, Array.prototype.slice.call(_args, 2, _args.length)); //Execute function with initial arguments, removing (fn & delay) - }, _delay); - }; - - this.add = function(extraDelay) { - this.pause(); - delay += extraDelay; - this.resume(); - }; - - this.resume(); - } - - - /** - * @desc PUBLIC - Makes Slot Machine animation effect - * @param DOM element - Html element - * @param object settings - Plugin configuration params - * @return jQuery node - Returns jQuery selector with some new functions (shuffle, stop, next, auto, active) - */ - function SlotMachine(element, options) { - this.element = element; - this.settings = $.extend({}, defaults, options); - this.defaults = defaults; - this.name = pluginName; - - //jQuery selector - this.$slot = $(element); - //Slot Machine elements - this.$tiles = this.$slot.children(); - //Container to wrap $tiles - this.$container = null; - //Min marginTop offset - this._minTop = null; - //Max marginTop offset - this._maxTop = null; - //First element (the last of the html container) - this._$fakeFirstTile = null; - //Last element (the first of the html container) - this._$fakeLastTile = null; - //Timeout recursive function to handle auto (settings.auto) - this._timer = null; - //Callback function - this._oncompleteStack = [this.settings.complete]; - //Number of spins left before stop - this._spinsLeft = null; - //Future result - this.futureActive = null; - //Machine is running? - this.isRunning = false; - //Machine is stopping? - this.isStopping = false; - //Current active element - this.active = this.settings.active; - - this.$slot.css('overflow', 'hidden'); - - //Validate active index - if (this.settings.active < 0 || this.settings.active >= this.$tiles.length) { - this.settings.active = 0; - this.active = 0; + // Required easing functions + if (typeof $.easing.easeOutBounce !== 'function') { + // From jQuery easing, extend jQuery animations functions + $.extend($.easing, { + easeOutBounce: function easeOutBounce(x, t, b, c, d) { + if ((t /= d) < 1 / 2.75) { + return c * (7.5625 * t * t) + b; + } else if (t < 2 / 2.75) { + return c * (7.5625 * (t -= 1.5 / 2.75) * t + 0.75) + b; + } else if (t < 2.5 / 2.75) { + return c * (7.5625 * (t -= 2.25 / 2.75) * t + 0.9375) + b; + } else { + return c * (7.5625 * (t -= 2.625 / 2.75) * t + 0.984375) + b; + } + } + }); } - //Wrap elements inside $container - this.$container = this.$tiles.wrapAll('
').parent(); - - //Set max top offset - this._maxTop = -this.$container.height(); - - //Add the last element behind the first to prevent the jump effect - this._$fakeFirstTile = this.$tiles.last().clone(); - this._$fakeLastTile = this.$tiles.first().clone(); - - this.$container.prepend(this._$fakeFirstTile); - this.$container.append(this._$fakeLastTile); - - //Set min top offset - this._minTop = -this._$fakeFirstTile.outerHeight(); - - //Direction parammeters - this._direction = { - selected: this.settings.direction === 'down' ? 'down' : 'up', - up: { - initial: this.getTileOffset(this.active), - first: 0, - last: this.getTileOffset(this.$tiles.length), - to: this._maxTop, - firstToLast: this.getTileOffset(this.$tiles.length), - lastToFirst: 0 - }, - down: { - initial: this.getTileOffset(this.active), - first: this.getTileOffset(this.$tiles.length), - last: 0, - to: this._minTop, - firstToLast: this.getTileOffset(this.$tiles.length), - lastToFirst: 0 - }, - get: function(param){return this[this.selected][param];} - }; - - //Show active element - this.$container.css('margin-top', this._direction.get('initial')); + var Timer = (function () { + function Timer(cb, delay) { + _classCallCheck(this, Timer); - //Start auto animation - if (this.settings.auto !== false) { - if (this.settings.auto === true) { - this.shuffle(); - } else { - this.auto(); - } - } - } - - /** - * @desc PUBLIC - Custom setTimeout using requestAnimationFrame - * @param function cb - Callback - * @param Number timeout - Timeout delay - */ - SlotMachine.prototype.raf = function(cb, timeout) { - var _raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame, - startTime = new Date().getTime(), - _rafHandler = function(/*timestamp*/){ - var drawStart = new Date().getTime(), - diff = drawStart - startTime; - - if(diff < timeout){ - _raf(_rafHandler); - }else if(typeof cb === 'function'){ - cb(); - } - }; - - _raf(_rafHandler); - }; - - /** - * @desc PUBLIC - Get element offset top - * @param Number index - Element position - * @return Number - Negative offset in px - */ - SlotMachine.prototype.getTileOffset = function(index) { - var offset = 0; - for (var i = 0; i < index; i++) { - offset += this.$tiles.eq(i).outerHeight(); - } - return -offset + this._minTop; - }; - - /** - * @desc PUBLIC - Get current showing element index - * @return Number - Element index - */ - SlotMachine.prototype.getVisibleTile = function() { - var firstTileHeight = this.$tiles.first().height(), - containerMarginTop = parseInt(this.$container.css('margin-top').replace(/px/, ''), 10); - - return Math.abs(Math.round(containerMarginTop / firstTileHeight)) - 1; - }; - - /** - * @desc PUBLIC - Changes randomize function - * @param function|Number - Set new randomize function - */ - SlotMachine.prototype.setRandomize = function(rnd) { - if (typeof rnd === 'number') { - this.settings.randomize = function() { - return rnd; - }; - } else { - this.settings.randomize = rnd; - } - }; - - /** - * @desc PUBLIC - Get random element different than last shown - * @param boolean cantBeTheCurrent - true||undefined if cant be choosen the current element, prevents repeat - * @return Number - Element index - */ - SlotMachine.prototype.getRandom = function(cantBeTheCurrent) { - var rnd, - removePrevious = cantBeTheCurrent || false; - do { - rnd = Math.floor(Math.random() * this.$tiles.length); - } while ((removePrevious && rnd === this.active) && rnd >= 0); - - return rnd; - }; - - /** - * @desc PUBLIC - Get random element based on the custom randomize function - * @return Number - Element index - */ - SlotMachine.prototype.getCustom = function() { - var choosen; - if (this.settings.randomize !== null && typeof this.settings.randomize === 'function') { - var index = this.settings.randomize.apply(this, [this.active]); - if (index < 0 || index >= this.$tiles.length) { - index = 0; - } - choosen = index; - } else { - choosen = this.getRandom(); - } - return choosen; - }; - - /** - * @desc PRIVATE - Get the previous element (no direction related) - * @return Number - Element index - */ - SlotMachine.prototype._getPrev = function() { - var prevIndex = (this.active - 1 < 0) ? (this.$tiles.length - 1) : (this.active - 1); - return prevIndex; - }; - - /** - * @desc PRIVATE - Get the next element (no direction related) - * @return Number - Element index - */ - SlotMachine.prototype._getNext = function() { - var nextIndex = (this.active + 1 < this.$tiles.length) ? (this.active + 1) : 0; - return nextIndex; - }; - - /** - * @desc PUBLIC - Get the previous element dor selected direction - * @return Number - Element index - */ - SlotMachine.prototype.getPrev = function() { - return this._direction.selected === 'up' ? this._getPrev() : this._getNext(); - }; - - /** - * @desc PUBLIC - Get the next element - * @return Number - Element index - */ - SlotMachine.prototype.getNext = function() { - return this._direction.selected === 'up' ? this._getNext() : this._getPrev(); - }; - - /** - * @desc PUBLIC - Set the spin direction - * @return Object - Direction data - */ - SlotMachine.prototype.setDirection = function(direction) { - if (this.isRunning) { - return; - } - this._direction.selected = direction === 'down' ? 'down' : 'up'; - return this._direction[this._direction.selected]; - }; - - /** - * @desc PRIVATE - Set CSS classes to make speed effect - * @param string FX_SPEED - Element speed [FX_FAST_BLUR||FX_NORMAL_BLUR||FX_SLOW_BLUR||FX_STOP] - * @param string||boolean fade - Set fade gradient effect - */ - SlotMachine.prototype._setAnimationFX = function(FX_SPEED, fade) { - this.raf(function() { - this.$tiles.removeClass([FX_FAST, FX_NORMAL, FX_SLOW].join(' ')).addClass(FX_SPEED); - - if (fade !== true || FX_SPEED === FX_STOP) { - this.$slot.add(this.$tiles).removeClass(FX_GRADIENT); - } else { - this.$slot.add(this.$tiles).addClass(FX_GRADIENT); - } - }.bind(this), this.settings.delay / 4); - }; - - /** - * @desc PRIVATE - Reset active element position - */ - SlotMachine.prototype._resetPosition = function() { - this.$container.css('margin-top', this._direction.get('initial')); - }; - - /** - * @desc PRIVATE - Checks if the machine is on the screen - * @return Number - Returns true if machine is on the screen - */ - SlotMachine.prototype.isVisible = function() { - //Stop animation if element is [above||below] screen, best for performance - var above = this.$slot.offset().top > $(window).scrollTop() + $(window).height(), - below = $(window).scrollTop() > this.$slot.height() + this.$slot.offset().top; - - return !above && !below; - }; - - /** - * @desc PUBLIC - SELECT previous element relative to the current active element - * @return Number - Returns result index - */ - SlotMachine.prototype.prev = function() { - this.futureActive = this.getPrev(); - this.isRunning = true; - this.stop(false); - return this.futureActive; - }; - - /** - * @desc PUBLIC - SELECT next element relative to the current active element - * @return Number - Returns result index - */ - SlotMachine.prototype.next = function() { - this.futureActive = this.getNext(); - this.isRunning = true; - this.stop(false); - return this.futureActive; - }; - - /** - * @desc PUBLIC - Starts shuffling the elements - * @param Number repeations - Number of shuffles (undefined to make infinite animation - * @return Number - Returns result index - */ - SlotMachine.prototype.shuffle = function(spins, onComplete) { - var self = this; - - if (onComplete !== undefined) { - this._oncompleteStack[1] = onComplete; - } + this.cb = cb; + this.initialDelay = delay; + this.delay = delay; + this.deferred = jQuery.Deferred(); + this.startTime = null; + this.timer = null; + this.running = false; - this.isRunning = true; - var delay = this.settings.delay; + this.resume(); - if (this.futureActive === null) { - //Get random or custom element - var rnd = this.getCustom(); - this.futureActive = rnd; - } + return this; + } - //Decreasing spin - if (typeof spins === 'number') { - //Change delay and speed - switch (spins) { - case 1: - case 2: - this._setAnimationFX(FX_SLOW, true); - break; - case 3: - case 4: - this._setAnimationFX(FX_NORMAL, true); - delay /= 1.5; - break; - default: - this._setAnimationFX(FX_FAST, true); - delay /= 2; - } - //Infinite spin - } else { - //Set animation effects - this._setAnimationFX(FX_FAST, true); - delay /= 2; - } + _createClass(Timer, [{ + key: '_start', + value: function _start() { + this.timer = setTimeout((function cb() { + this.cb.call(this); + }).bind(this), this.delay); + } + }, { + key: 'cancel', + value: function cancel() { + this.running = false; + clearTimeout(this.timer); + } + }, { + key: 'pause', + value: function pause() { + if (this.running) { + this.delay -= new Date().getTime() - this.startTime; + this.cancel(); + } + } + }, { + key: 'resume', + value: function resume() { + if (!this.running) { + this.running = true; + this.startTime = new Date().getTime(); + + this._start(); + } + } + }, { + key: 'reset', + value: function reset() { + this.cancel(); + this.delay = this.initialDelay; + this._start(); + } + }, { + key: 'add', + value: function add(extraDelay) { + this.pause(); + this.delay += extraDelay; + this.resume(); + } + }]); + + return Timer; + })(); + + /** + * @desc Class - Makes Slot Machine animation effect + * @param DOM element - Html element + * @param object settings - Plugin configuration params + * @return jQuery node - Returns jQuery selector with some new functions (shuffle, stop, next, auto, active) + */ + + var SlotMachine = (function () { + function SlotMachine(element, options) { + _classCallCheck(this, SlotMachine); + + this.element = element; + this.settings = $.extend({}, defaults, options); + this.defaults = defaults; + this.name = pluginName; + + // jQuery selector + this.$slot = $(element); + // Slot Machine elements + this.$tiles = this.$slot.children(); + // Container to wrap $tiles + this.$container = null; + // Min marginTop offset + this._minTop = null; + // Max marginTop offset + this._maxTop = null; + // First element (the last of the html container) + this._$fakeFirstTile = null; + // Last element (the first of the html container) + this._$fakeLastTile = null; + // Timeout recursive function to handle auto (settings.auto) + this._timer = null; + // Callback function + this._oncompleteStack = [this.settings.complete]; + // Number of spins left before stop + this._spinsLeft = null; + // Future result + this.futureActive = null; + // Machine is running? + this.running = false; + // Machine is stopping? + this.stopping = false; + // Current active element + this.active = this.settings.active; + + this.$slot.css('overflow', 'hidden'); + + // Wrap elements inside $container + this.$container = this.$tiles.wrapAll('
').parent(); + + // Set max top offset + this._maxTop = -this.$container.height(); + + // Create fake tiles to prevent empty offset + this._initFakeTiles(); + + // Set min top offset + this._minTop = -this._$fakeFirstTile.outerHeight(); + + // Initialize spin direction [up, down] + this._initDirection(); + + // Show active element + this._marginTop = this.direction.initial; + + // Start auto animation + if (this.settings.auto !== false) { + if (this.settings.auto === true) { + this.shuffle(); + } else { + this.auto(); + } + } + } - //Perform animation - if (!this.isVisible() && this.settings.stopHidden === true) { - spins = 0; - self.stop(); - } else { - this.$container.animate({ - marginTop: this._direction.get('to') - }, delay, 'linear', function() { - //Reset top position - self.$container.css('margin-top', self._direction.get('first')); - - if (spins - 1 <= 0) { - self.stop(); + _createClass(SlotMachine, [{ + key: '_initFakeTiles', + value: function _initFakeTiles() { + // Add the last element behind the first to prevent the jump effect + this._$fakeFirstTile = this.$tiles.last().clone(); + this._$fakeLastTile = this.$tiles.first().clone(); + // Add fake titles to the DOM + this.$container.prepend(this._$fakeFirstTile); + this.$container.append(this._$fakeLastTile); + } + }, { + key: '_initDirection', + value: function _initDirection() { + this._direction = { + selected: this.settings.direction === 'down' ? 'down' : 'up', + up: { + key: 'up', + initial: this.getTileOffset(this.active), + first: 0, + last: this.getTileOffset(this.$tiles.length), + to: this._maxTop, + firstToLast: this.getTileOffset(this.$tiles.length), + lastToFirst: 0 + }, + down: { + key: 'down', + initial: this.getTileOffset(this.active), + first: this.getTileOffset(this.$tiles.length), + last: 0, + to: this._minTop, + firstToLast: this.getTileOffset(this.$tiles.length), + lastToFirst: 0 + } + }; + } + + /** + * @desc PUBLIC - Get active element + */ + + }, { + key: 'raf', + + /** + * @desc PUBLIC - Custom setTimeout using requestAnimationFrame + * @param function cb - Callback + * @param {Number} timeout - Timeout delay + */ + value: function raf(cb, timeout) { + var _raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame, + startTime = new Date().getTime(), + _rafHandler = function _rafHandler() { + var drawStart = new Date().getTime(), + diff = drawStart - startTime; + + if (diff < timeout) { + _raf(_rafHandler); + } else if (typeof cb === 'function') { + cb(); + } + }; + + _raf(_rafHandler); + } + + /** + * @desc PUBLIC - Get element offset top + * @param {Number} index - Element position + * @return {Number} - Negative offset in px + */ + + }, { + key: 'getTileOffset', + value: function getTileOffset(index) { + var offset = 0; + + for (var i = 0; i < index; i++) { + offset += this.$tiles.eq(i).outerHeight(); + } + + return this._minTop - offset; + } + + /** + * @desc PRIVATE - Reset active element position + */ + + }, { + key: '_resetPosition', + value: function _resetPosition() { + this._marginTop = this.direction.initial; + } + + /** + * @desc PUBLIC - Changes randomize function + * @param function|Number - Set new randomize function + */ + + }, { + key: 'setRandomize', + value: function setRandomize(rnd) { + this.settings.randomize = rnd; + + if (typeof rnd === 'number') { + this.settings.randomize = function () { + return rnd; + }; + } + } + + /** + * @desc PUBLIC - SELECT previous element relative to the current active element + * @return {Number} - Returns result index + */ + + }, { + key: 'prev', + value: function prev() { + this.futureActive = this.prevIndex; + this.running = true; + this.stop(false); + + return this.futureActive; + } + + /** + * @desc PUBLIC - SELECT next element relative to the current active element + * @return {Number} - Returns result index + */ + + }, { + key: 'next', + value: function next() { + this.futureActive = this.nextIndex; + this.running = true; + this.stop(false); + + return this.futureActive; + } + + /** + * @desc PUBLIC - Starts shuffling the elements + * @param {Number} repeations - Number of shuffles (undefined to make infinite animation + * @return {Number} - Returns result index + */ + + }, { + key: 'shuffle', + value: function shuffle(spins, onComplete) { + var delay = this.settings.delay; + + if (onComplete) { + this._oncompleteStack[1] = onComplete; + } + if (this.futureActive === null) { + // Get random or custom element + this.futureActive = this.custom; + } + this.running = true; + this._fade = true; + + // Decreasing spin + if (typeof spins === 'number') { + // Change delay and speed + switch (spins) { + case 1: + case 2: + this._animationFX = FX_SLOW; + break; + case 3: + case 4: + this._animationFX = FX_NORMAL; + delay /= 1.5; + break; + default: + this._animationFX = FX_FAST; + delay /= 2; + } + // Infinite spin + } else { + // Set animation effects + this._animationFX = FX_FAST; + delay /= 2; + } + + // Perform animation + if (!this.visible && this.settings.stopHidden === true) { + this.stop(); + } else { + this.$container.animate({ + marginTop: this.direction.to + }, delay, 'linear', (function cb() { + // Reset top position + this._marginTop = this.direction.first; + + if (spins - 1 <= 0) { + this.stop(); + } else { + // Repeat animation + this.shuffle(spins - 1); + } + }).bind(this)); + } + + return this.futureActive; + } + + /** + * @desc PUBLIC - Stop shuffling the elements + * @return {Number} - Returns result index + */ + + }, { + key: 'stop', + value: function stop(showGradient) { + if (!this.running) { + return; + } else if (this.stopping) { + return this.futureActive; + } + + // Stop animation NOW!!!!!!! + this.$container.clearQueue().stop(true, false); + + this._fade = showGradient === undefined ? true : showGradient; + this._animationFX = FX_SLOW; + this.running = true; + this.stopping = true; + // Set current active element + this.active = this.visibleTile; + + // Check direction to prevent jumping + if (this.futureActive > this.active) { + // We are moving to the prev (first to last) + if (this.active === 0 && this.futureActive === this.$tiles.length - 1) { + this._marginTop = this.direction.firstToLast; + } + // We are moving to the next (last to first) + } else if (this.active === this.$tiles.length - 1 && this.futureActive === 0) { + this._marginTop = this.direction.lastToFirst; + } + + // Update last choosen element index + this.active = this.futureActive; + + // Get delay + var delay = this.settings.delay * 3; + + // Perform animation + this.$container.animate({ + marginTop: this.getTileOffset(this.active) + }, delay, 'easeOutBounce', (function cb() { + + this.stopping = false; + this.running = false; + this.futureActive = null; + + if (typeof this._oncompleteStack[0] === 'function') { + this._oncompleteStack[0].apply(this, [this.active]); + } + if (typeof this._oncompleteStack[1] === 'function') { + this._oncompleteStack[1].apply(this, [this.active]); + } + }).bind(this)); + + // Disable blur + this.raf((function cb() { + this._fade = false; + this._animationFX = FX_STOP; + }).bind(this), delay / 1.75); + + return this.active; + } + + /** + * @desc PUBLIC - Start auto shufflings, animation stops each 3 repeations. Then restart animation recursively + */ + + }, { + key: 'auto', + value: function auto() { + if (!this.running) { + this._timer = new Timer((function cb() { + if (typeof this.settings.randomize !== 'function') { + this.futureActive = this.next; + } + if (!this.visible && this.settings.stopHidden === true) { + this.raf((function cb2() { + this._timer.reset(); + }).bind(this), 500); + } else { + this.shuffle(this.settings.spins, (function cb2() { + this._timer.reset(); + }).bind(this)); + } + }).bind(this), this.settings.auto); + } + } + }, { + key: 'active', + get: function get() { + return this._active; + } + + /** + * @desc PUBLIC - Get current showing element index + * @return {Number} - Element index + */ + , + + /** + * @desc PUBLIC - Set active element + * @param {Number} - Active element index + */ + set: function set(index) { + this._active = index; + if (index < 0 || index >= this.$tiles.length) { + this._active = 0; + } + } + + /** + * @desc PUBLIC - Set the spin direction + */ + + }, { + key: 'visibleTile', + get: function get() { + var firstTileHeight = this.$tiles.first().height(), + rawContainerMargin = this.$container.css('margin-top'), + containerMargin = parseInt(rawContainerMargin.replace(/px/, ''), 10); + + return Math.abs(Math.round(containerMargin / firstTileHeight)) - 1; + } + + /** + * @desc PUBLIC - Get random element different than last shown + * @param {Boolean} cantBeTheCurrent - true||undefined if cant be choosen the current element, prevents repeat + * @return {Number} - Element index + */ + + }, { + key: 'random', + get: function get() { + return Math.floor(Math.random() * this.$tiles.length); + } + + /** + * @desc PUBLIC - Get random element based on the custom randomize function + * @return {Number} - Element index + */ + + }, { + key: 'custom', + get: function get() { + var choosen = void 0; + + if (typeof this.settings.randomize === 'function') { + var index = this.settings.randomize.call(this, this.active); + if (index < 0 || index >= this.$tiles.length) { + index = 0; + } + choosen = index; + } else { + choosen = this.random; + } + + return choosen; + } + + /** + * @desc PUBLIC - Get the spin direction + */ + + }, { + key: 'direction', + get: function get() { + return this._direction[this._direction.selected]; + } + + /** + * @desc PRIVATE - Get the previous element (no direction related) + * @return {Number} - Element index + */ + , + set: function set(direction) { + if (!this.running) { + this.direction = direction === 'down' ? 'down' : 'up'; + } + } + + /** + * @desc PRIVATE - Set CSS speed cclass + * @param string FX_SPEED - Element speed [FX_FAST_BLUR||FX_NORMAL_BLUR||FX_SLOW_BLUR||FX_STOP] + */ + + }, { + key: '_prevIndex', + get: function get() { + var prevIndex = this.active - 1; + + return prevIndex < 0 ? this.$tiles.length - 1 : prevIndex; + } + + /** + * @desc PRIVATE - Get the next element (no direction related) + * @return {Number} - Element index + */ + + }, { + key: '_nextIndex', + get: function get() { + var nextIndex = this.active + 1; + + return nextIndex < this.$tiles.length ? nextIndex : 0; + } + + /** + * @desc PUBLIC - Get the previous element dor selected direction + * @return {Number} - Element index + */ + + }, { + key: 'prevIndex', + get: function get() { + return this.direction === 'up' ? this._prevIndex : this._nextIndex; + } + + /** + * @desc PUBLIC - Get the next element + * @return {Number} - Element index + */ + + }, { + key: 'nextIndex', + get: function get() { + return this.direction === 'up' ? this._nextIndex : this._prevIndex; + } + + /** + * Stop animation if element is [above||below] screen, best for performance + * @desc PRIVATE - Checks if the machine is on the screen + * @return {Number} - Returns true if machine is on the screen + */ + + }, { + key: 'visible', + get: function get() { + var $window = $(window), + above = this.$slot.offset().top > $window.scrollTop() + $window.height(), + below = $window.scrollTop() > this.$slot.height() + this.$slot.offset().top; + + // Stop animation if element is [above||below] screen, best for performance + return !above && !below; + } + }, { + key: '_fxClass', + set: function set(FX_SPEED) { + var classes = [FX_FAST, FX_NORMAL, FX_SLOW].join(' '); + + this.$tiles.removeClass(classes).addClass(FX_SPEED); + } + + /** + * @desc PRIVATE - Set CSS classes to make speed effect + * @param string FX_SPEED - Element speed [FX_FAST_BLUR||FX_NORMAL_BLUR||FX_SLOW_BLUR||FX_STOP] + * @param string||boolean fade - Set fade gradient effect + */ + + }, { + key: '_animationFX', + set: function set(FX_SPEED) { + var delay = this.settings.delay / 4, + $elements = this.$slot.add(this.$tiles); + + this.raf((function cb() { + this._fxClass = FX_SPEED; + + if (this.fade !== true || FX_SPEED === FX_STOP) { + $elements.removeClass(FX_GRADIENT); + } else { + $elements.addClass(FX_GRADIENT); + } + }).bind(this), delay); + } + + /** + * @desc PRIVATE - Set container margin + * @param {Number}||String - Active element index + */ + + }, { + key: '_marginTop', + set: function set(margin) { + this.$container.css('margin-top', margin); + } + }]); + + return SlotMachine; + })(); + + /* + * Create new plugin instance if needed and return it + */ + + function _getInstance(element, options) { + var machine = void 0; + if (!$.data(element[0], 'plugin_' + pluginName)) { + machine = new SlotMachine(element, options); + $.data(element[0], 'plugin_' + pluginName, machine); } else { - //Repeat animation - self.shuffle(spins - 1); + machine = $.data(element[0], 'plugin_' + pluginName); } - }); - } - - return this.futureActive; - }; - - /** - * @desc PUBLIC - Stop shuffling the elements - * @return Number - Returns result index - */ - SlotMachine.prototype.stop = function(showGradient) { - if (!this.isRunning) { - return; - } else if (this.isStopping) { - return this.futureActive; - } - var self = this; - - //Stop animation NOW!!!!!!! - this.$container.clearQueue().stop(true, false); - - this._setAnimationFX(FX_SLOW, showGradient === undefined ? true : showGradient); - - this.isRunning = true; - this.isStopping = true; - - //Set current active element - this.active = this.getVisibleTile(); - - //Check direction to prevent jumping - if (this.futureActive > this.active) { - //We are moving to the prev (first to last) - if (this.active === 0 && this.futureActive === this.$tiles.length - 1) { - this.$container.css('margin-top', this._direction.get('firstToLast')); - } - } else { - //We are moving to the next (last to first) - if (this.active === this.$tiles.length - 1 && this.futureActive === 0) { - this.$container.css('margin-top', this._direction.get('lastToFirst')); - } + return machine; } - //Update last choosen element index - this.active = this.futureActive; - - //Get delay - var delay = this.settings.delay * 3; - - //Perform animation - this.$container.animate({ - marginTop: this.getTileOffset(this.active) - }, delay, 'easeOutBounce', function() { - - self.isStopping = false; - self.isRunning = false; - self.futureActive = null; - - if (typeof self._oncompleteStack[0] === 'function') { - self._oncompleteStack[0].apply(self, [self.active]); - } - if (typeof self._oncompleteStack[1] === 'function') { - self._oncompleteStack[1].apply(self, [self.active]); - } - }); - - //Disable blur - this.raf(function() { - this._setAnimationFX(FX_STOP, false); - }.bind(this), delay / 1.75); - - return this.active; - }; - - /** - * @desc PUBLIC - Start auto shufflings, animation stops each 3 repeations. Then restart animation recursively - */ - SlotMachine.prototype.auto = function() { - var self = this; - - this._timer = new Timer(function() { - if (typeof self.settings.randomize !== 'function') { - self.futureActive = self.getNext(); - } - self.isRunning = true; - if (!self.isVisible() && self.settings.stopHidden === true) { - self.raf(function() { - self._timer.reset(); - }, 500); - } else { - self.shuffle(self.settings.spins, function() { - self._timer.reset(); - }); - } - - }, this.settings.auto); - }; - - /* - * Create new plugin instance if needed and return it - */ - function _getInstance(element, options) { - var machine; - if (!$.data(element[0], 'plugin_' + pluginName)) { - machine = new SlotMachine(element, options); - $.data(element[0], 'plugin_' + pluginName, machine); - } else { - machine = $.data(element[0], 'plugin_' + pluginName); - } - return machine; - } - - /* - * Chainable instance - */ - $.fn[pluginName] = function(options) { - if (this.length === 1) { - return _getInstance(this, options); - } else { - var $els = this; - return $.map($els, function(el, index) { - var $el = $els.eq(index); - return _getInstance($el, options); - }); - } - }; + /* + * Chainable instance + */ + $.fn[pluginName] = function initPlugin(options) { + var _this = this; + var instances = void 0; + if (this.length === 1) { + instances = _getInstance(this, options); + } else { + (function () { + var $els = _this; + instances = $.map($els, function mapValue(el, index) { + var $el = $els.eq(index); + return _getInstance($el, options); + }); + })(); + } + return instances; + }; })(jQuery, window, document); diff --git a/dist/jquery.slotmachine.min.js b/dist/jquery.slotmachine.min.js index f350433..c98df63 100644 --- a/dist/jquery.slotmachine.min.js +++ b/dist/jquery.slotmachine.min.js @@ -1,4 +1,4 @@ -/*! SlotMachine - v2.2.0 - 2015-11-05 +/*! SlotMachine - v2.3.0 - 2015-12-22 * https://github.com/josex2r/jQuery-SlotMachine * Copyright (c) 2015 Jose Luis Represa; Licensed MIT */ -!function(a,b,c,d){function e(a,b){var c,d,e=this,f=a,g=arguments,h=b;this.running=!1,this.onpause=function(){},this.onresume=function(){},this.cancel=function(){this.running=!1,clearTimeout(d)},this.pause=function(){this.running&&(b-=(new Date).getTime()-c,this.cancel(),this.onpause())},this.resume=function(){this.running||(this.running=!0,c=(new Date).getTime(),d=setTimeout(function(){f.apply(e,Array.prototype.slice.call(g,2,g.length))},b),this.onresume())},this.reset=function(){this.cancel(),this.running=!0,b=h,d=setTimeout(function(){f.apply(e,Array.prototype.slice.call(g,2,g.length))},h)},this.add=function(a){this.pause(),b+=a,this.resume()},this.resume()}function f(b,c){this.element=b,this.settings=a.extend({},i,c),this.defaults=i,this.name=h,this.$slot=a(b),this.$tiles=this.$slot.children(),this.$container=null,this._minTop=null,this._maxTop=null,this._$fakeFirstTile=null,this._$fakeLastTile=null,this._timer=null,this._oncompleteStack=[this.settings.complete],this._spinsLeft=null,this.futureActive=null,this.isRunning=!1,this.isStopping=!1,this.active=this.settings.active,this.$slot.css("overflow","hidden"),(this.settings.active<0||this.settings.active>=this.$tiles.length)&&(this.settings.active=0,this.active=0),this.$container=this.$tiles.wrapAll('
').parent(),this._maxTop=-this.$container.height(),this._$fakeFirstTile=this.$tiles.last().clone(),this._$fakeLastTile=this.$tiles.first().clone(),this.$container.prepend(this._$fakeFirstTile),this.$container.append(this._$fakeLastTile),this._minTop=-this._$fakeFirstTile.outerHeight(),this._direction={selected:"down"===this.settings.direction?"down":"up",up:{initial:this.getTileOffset(this.active),first:0,last:this.getTileOffset(this.$tiles.length),to:this._maxTop,firstToLast:this.getTileOffset(this.$tiles.length),lastToFirst:0},down:{initial:this.getTileOffset(this.active),first:this.getTileOffset(this.$tiles.length),last:0,to:this._minTop,firstToLast:this.getTileOffset(this.$tiles.length),lastToFirst:0},get:function(a){return this[this.selected][a]}},this.$container.css("margin-top",this._direction.get("initial")),this.settings.auto!==!1&&(this.settings.auto===!0?this.shuffle():this.auto())}function g(b,c){var d;return a.data(b[0],"plugin_"+h)?d=a.data(b[0],"plugin_"+h):(d=new f(b,c),a.data(b[0],"plugin_"+h,d)),d}var h="slotMachine",i={active:0,delay:200,auto:!1,spins:5,randomize:null,complete:null,stopHidden:!0,direction:"up"},j="slotMachineBlurFast",k="slotMachineBlurMedium",l="slotMachineBlurSlow",m="slotMachineGradient",n=m;a(c).ready(function(){var b='#slotMachineBlurFilterFast',c='#slotMachineBlurFilterMedium',d='#slotMachineBlurFilterSlow',e='#slotMachineFadeMask';a("body").append("')}),"function"!=typeof a.easing.easeOutBounce&&a.extend(a.easing,{easeOutBounce:function(a,b,c,d,e){return(b/=e)<1/2.75?d*(7.5625*b*b)+c:2/2.75>b?d*(7.5625*(b-=1.5/2.75)*b+.75)+c:2.5/2.75>b?d*(7.5625*(b-=2.25/2.75)*b+.9375)+c:d*(7.5625*(b-=2.625/2.75)*b+.984375)+c}}),f.prototype.raf=function(a,c){var d=b.requestAnimationFrame||b.mozRequestAnimationFrame||b.webkitRequestAnimationFrame||b.msRequestAnimationFrame,e=(new Date).getTime(),f=function(){var b=(new Date).getTime(),g=b-e;c>g?d(f):"function"==typeof a&&a()};d(f)},f.prototype.getTileOffset=function(a){for(var b=0,c=0;a>c;c++)b+=this.$tiles.eq(c).outerHeight();return-b+this._minTop},f.prototype.getVisibleTile=function(){var a=this.$tiles.first().height(),b=parseInt(this.$container.css("margin-top").replace(/px/,""),10);return Math.abs(Math.round(b/a))-1},f.prototype.setRandomize=function(a){"number"==typeof a?this.settings.randomize=function(){return a}:this.settings.randomize=a},f.prototype.getRandom=function(a){var b,c=a||!1;do b=Math.floor(Math.random()*this.$tiles.length);while(c&&b===this.active&&b>=0);return b},f.prototype.getCustom=function(){var a;if(null!==this.settings.randomize&&"function"==typeof this.settings.randomize){var b=this.settings.randomize.apply(this,[this.active]);(0>b||b>=this.$tiles.length)&&(b=0),a=b}else a=this.getRandom();return a},f.prototype._getPrev=function(){var a=this.active-1<0?this.$tiles.length-1:this.active-1;return a},f.prototype._getNext=function(){var a=this.active+1a(b).scrollTop()+a(b).height(),d=a(b).scrollTop()>this.$slot.height()+this.$slot.offset().top;return!c&&!d},f.prototype.prev=function(){return this.futureActive=this.getPrev(),this.isRunning=!0,this.stop(!1),this.futureActive},f.prototype.next=function(){return this.futureActive=this.getNext(),this.isRunning=!0,this.stop(!1),this.futureActive},f.prototype.shuffle=function(a,b){var c=this;b!==d&&(this._oncompleteStack[1]=b),this.isRunning=!0;var e=this.settings.delay;if(null===this.futureActive){var f=this.getCustom();this.futureActive=f}if("number"==typeof a)switch(a){case 1:case 2:this._setAnimationFX(l,!0);break;case 3:case 4:this._setAnimationFX(k,!0),e/=1.5;break;default:this._setAnimationFX(j,!0),e/=2}else this._setAnimationFX(j,!0),e/=2;return this.isVisible()||this.settings.stopHidden!==!0?this.$container.animate({marginTop:this._direction.get("to")},e,"linear",function(){c.$container.css("margin-top",c._direction.get("first")),0>=a-1?c.stop():c.shuffle(a-1)}):(a=0,c.stop()),this.futureActive},f.prototype.stop=function(a){if(this.isRunning){if(this.isStopping)return this.futureActive;var b=this;this.$container.clearQueue().stop(!0,!1),this._setAnimationFX(l,a===d?!0:a),this.isRunning=!0,this.isStopping=!0,this.active=this.getVisibleTile(),this.futureActive>this.active?0===this.active&&this.futureActive===this.$tiles.length-1&&this.$container.css("margin-top",this._direction.get("firstToLast")):this.active===this.$tiles.length-1&&0===this.futureActive&&this.$container.css("margin-top",this._direction.get("lastToFirst")),this.active=this.futureActive;var c=3*this.settings.delay;return this.$container.animate({marginTop:this.getTileOffset(this.active)},c,"easeOutBounce",function(){b.isStopping=!1,b.isRunning=!1,b.futureActive=null,"function"==typeof b._oncompleteStack[0]&&b._oncompleteStack[0].apply(b,[b.active]),"function"==typeof b._oncompleteStack[1]&&b._oncompleteStack[1].apply(b,[b.active])}),this.raf(function(){this._setAnimationFX(n,!1)}.bind(this),c/1.75),this.active}},f.prototype.auto=function(){var a=this;this._timer=new e(function(){"function"!=typeof a.settings.randomize&&(a.futureActive=a.getNext()),a.isRunning=!0,a.isVisible()||a.settings.stopHidden!==!0?a.shuffle(a.settings.spins,function(){a._timer.reset()}):a.raf(function(){a._timer.reset()},500)},this.settings.auto)},a.fn[h]=function(b){if(1===this.length)return g(this,b);var c=this;return a.map(c,function(a,d){var e=c.eq(d);return g(e,b)})}}(jQuery,window,document); \ No newline at end of file +"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function a(a,b){for(var c=0;c"+("."+h+'{-webkit-filter: blur(5px);-moz-filter: blur(5px);-o-filter: blur(5px);-ms-filter: blur(5px);filter: blur(5px);filter: url("data:image/svg+xml;utf8,'+b+'");filter:progid:DXImageTransform.Microsoft.Blur(PixelRadius="5")}')+("."+i+'{-webkit-filter: blur(3px);-moz-filter: blur(3px);-o-filter: blur(3px);-ms-filter: blur(3px);filter: blur(3px);filter: url("data:image/svg+xml;utf8,'+c+'");filter:progid:DXImageTransform.Microsoft.Blur(PixelRadius="3")}')+("."+j+'{-webkit-filter: blur(1px);-moz-filter: blur(1px);-o-filter: blur(1px);-ms-filter: blur(1px);filter: blur(1px);filter: url("data:image/svg+xml;utf8,'+d+'");filter:progid:DXImageTransform.Microsoft.Blur(PixelRadius="1")}')+("."+k+"{")+"-webkit-mask-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(0,0,0,0)), color-stop(25%, rgba(0,0,0,1)), color-stop(75%, rgba(0,0,0,1)), color-stop(100%, rgba(0,0,0,0)) );"+('mask: url("data:image/svg+xml;utf8,'+e+'");')+"}")}),"function"!=typeof a.easing.easeOutBounce&&a.extend(a.easing,{easeOutBounce:function(a,b,c,d,e){return(b/=e)<1/2.75?d*(7.5625*b*b)+c:2/2.75>b?d*(7.5625*(b-=1.5/2.75)*b+.75)+c:2.5/2.75>b?d*(7.5625*(b-=2.25/2.75)*b+.9375)+c:d*(7.5625*(b-=2.625/2.75)*b+.984375)+c}});var m=function(){function a(b,c){return _classCallCheck(this,a),this.cb=b,this.initialDelay=c,this.delay=c,this.deferred=jQuery.Deferred(),this.startTime=null,this.timer=null,this.running=!1,this.resume(),this}return _createClass(a,[{key:"_start",value:function(){this.timer=setTimeout(function(){this.cb.call(this)}.bind(this),this.delay)}},{key:"cancel",value:function(){this.running=!1,clearTimeout(this.timer)}},{key:"pause",value:function(){this.running&&(this.delay-=(new Date).getTime()-this.startTime,this.cancel())}},{key:"resume",value:function(){this.running||(this.running=!0,this.startTime=(new Date).getTime(),this._start())}},{key:"reset",value:function(){this.cancel(),this.delay=this.initialDelay,this._start()}},{key:"add",value:function(a){this.pause(),this.delay+=a,this.resume()}}]),a}(),n=function(){function c(b,d){_classCallCheck(this,c),this.element=b,this.settings=a.extend({},g,d),this.defaults=g,this.name=f,this.$slot=a(b),this.$tiles=this.$slot.children(),this.$container=null,this._minTop=null,this._maxTop=null,this._$fakeFirstTile=null,this._$fakeLastTile=null,this._timer=null,this._oncompleteStack=[this.settings.complete],this._spinsLeft=null,this.futureActive=null,this.running=!1,this.stopping=!1,this.active=this.settings.active,this.$slot.css("overflow","hidden"),this.$container=this.$tiles.wrapAll('
').parent(),this._maxTop=-this.$container.height(),this._initFakeTiles(),this._minTop=-this._$fakeFirstTile.outerHeight(),this._initDirection(),this._marginTop=this.direction.initial,this.settings.auto!==!1&&(this.settings.auto===!0?this.shuffle():this.auto())}return _createClass(c,[{key:"_initFakeTiles",value:function(){this._$fakeFirstTile=this.$tiles.last().clone(),this._$fakeLastTile=this.$tiles.first().clone(),this.$container.prepend(this._$fakeFirstTile),this.$container.append(this._$fakeLastTile)}},{key:"_initDirection",value:function(){this._direction={selected:"down"===this.settings.direction?"down":"up",up:{key:"up",initial:this.getTileOffset(this.active),first:0,last:this.getTileOffset(this.$tiles.length),to:this._maxTop,firstToLast:this.getTileOffset(this.$tiles.length),lastToFirst:0},down:{key:"down",initial:this.getTileOffset(this.active),first:this.getTileOffset(this.$tiles.length),last:0,to:this._minTop,firstToLast:this.getTileOffset(this.$tiles.length),lastToFirst:0}}}},{key:"raf",value:function(a,c){var d=b.requestAnimationFrame||b.mozRequestAnimationFrame||b.webkitRequestAnimationFrame||b.msRequestAnimationFrame,e=(new Date).getTime(),f=function g(){var b=(new Date).getTime(),f=b-e;c>f?d(g):"function"==typeof a&&a()};d(f)}},{key:"getTileOffset",value:function(a){for(var b=0,c=0;a>c;c++)b+=this.$tiles.eq(c).outerHeight();return this._minTop-b}},{key:"_resetPosition",value:function(){this._marginTop=this.direction.initial}},{key:"setRandomize",value:function(a){this.settings.randomize=a,"number"==typeof a&&(this.settings.randomize=function(){return a})}},{key:"prev",value:function(){return this.futureActive=this.prevIndex,this.running=!0,this.stop(!1),this.futureActive}},{key:"next",value:function(){return this.futureActive=this.nextIndex,this.running=!0,this.stop(!1),this.futureActive}},{key:"shuffle",value:function(a,b){var c=this.settings.delay;if(b&&(this._oncompleteStack[1]=b),null===this.futureActive&&(this.futureActive=this.custom),this.running=!0,this._fade=!0,"number"==typeof a)switch(a){case 1:case 2:this._animationFX=j;break;case 3:case 4:this._animationFX=i,c/=1.5;break;default:this._animationFX=h,c/=2}else this._animationFX=h,c/=2;return this.visible||this.settings.stopHidden!==!0?this.$container.animate({marginTop:this.direction.to},c,"linear",function(){this._marginTop=this.direction.first,0>=a-1?this.stop():this.shuffle(a-1)}.bind(this)):this.stop(),this.futureActive}},{key:"stop",value:function(a){if(this.running){if(this.stopping)return this.futureActive;this.$container.clearQueue().stop(!0,!1),this._fade=a===d?!0:a,this._animationFX=j,this.running=!0,this.stopping=!0,this.active=this.visibleTile,this.futureActive>this.active?0===this.active&&this.futureActive===this.$tiles.length-1&&(this._marginTop=this.direction.firstToLast):this.active===this.$tiles.length-1&&0===this.futureActive&&(this._marginTop=this.direction.lastToFirst),this.active=this.futureActive;var b=3*this.settings.delay;return this.$container.animate({marginTop:this.getTileOffset(this.active)},b,"easeOutBounce",function(){this.stopping=!1,this.running=!1,this.futureActive=null,"function"==typeof this._oncompleteStack[0]&&this._oncompleteStack[0].apply(this,[this.active]),"function"==typeof this._oncompleteStack[1]&&this._oncompleteStack[1].apply(this,[this.active])}.bind(this)),this.raf(function(){this._fade=!1,this._animationFX=l}.bind(this),b/1.75),this.active}}},{key:"auto",value:function(){this.running||(this._timer=new m(function(){"function"!=typeof this.settings.randomize&&(this.futureActive=this.next),this.visible||this.settings.stopHidden!==!0?this.shuffle(this.settings.spins,function(){this._timer.reset()}.bind(this)):this.raf(function(){this._timer.reset()}.bind(this),500)}.bind(this),this.settings.auto))}},{key:"active",get:function(){return this._active},set:function(a){this._active=a,(0>a||a>=this.$tiles.length)&&(this._active=0)}},{key:"visibleTile",get:function(){var a=this.$tiles.first().height(),b=this.$container.css("margin-top"),c=parseInt(b.replace(/px/,""),10);return Math.abs(Math.round(c/a))-1}},{key:"random",get:function(){return Math.floor(Math.random()*this.$tiles.length)}},{key:"custom",get:function(){var a=void 0;if("function"==typeof this.settings.randomize){var b=this.settings.randomize.call(this,this.active);(0>b||b>=this.$tiles.length)&&(b=0),a=b}else a=this.random;return a}},{key:"direction",get:function(){return this._direction[this._direction.selected]},set:function(a){this.running||(this.direction="down"===a?"down":"up")}},{key:"_prevIndex",get:function(){var a=this.active-1;return 0>a?this.$tiles.length-1:a}},{key:"_nextIndex",get:function(){var a=this.active+1;return ac.scrollTop()+c.height(),e=c.scrollTop()>this.$slot.height()+this.$slot.offset().top;return!d&&!e}},{key:"_fxClass",set:function(a){var b=[h,i,j].join(" ");this.$tiles.removeClass(b).addClass(a)}},{key:"_animationFX",set:function(a){var b=this.settings.delay/4,c=this.$slot.add(this.$tiles);this.raf(function(){this._fxClass=a,this.fade!==!0||a===l?c.removeClass(k):c.addClass(k)}.bind(this),b)}},{key:"_marginTop",set:function(a){this.$container.css("margin-top",a)}}]),c}();a.fn[f]=function(b){var c=this,d=void 0;return 1===this.length?d=e(this,b):!function(){var f=c;d=a.map(f,function(a,c){var d=f.eq(c);return e(d,b)})}(),d}}(jQuery,window,document); \ No newline at end of file diff --git a/package.json b/package.json index 964dab2..b5e14c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery-slotmachine", - "version": "2.2.0", + "version": "2.3.0", "engines": { "node": ">= 0.8.0" }, diff --git a/slotmachine.jquery.json b/slotmachine.jquery.json index d3ebe42..f56cc00 100644 --- a/slotmachine.jquery.json +++ b/slotmachine.jquery.json @@ -11,7 +11,7 @@ "winning", "machine" ], - "version": "2.2.0", + "version": "2.3.0", "download": "https://github.com/josex2r/jQuery-SlotMachine", "homepage": "https://github.com/josex2r/jQuery-SlotMachine", "demo": "http://josex2r.github.io/jQuery-SlotMachine/",