Skip to content

Commit 6d7b15c

Browse files
committed
New feature! Animation direction
1 parent 8b6c03e commit 6d7b15c

File tree

7 files changed

+162
-53
lines changed

7 files changed

+162
-53
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,41 +76,47 @@ Change spin result, if the returned value is out of bounds, the element will be
7676
machine.setRandomize(foo); //foo must be a function (should return int) or an int
7777
```
7878

79+
Change spin direction, machine must not be running:
80+
81+
```javascript
82+
machine.setDirection(direction); //direction must be a String ('up' || 'down')
83+
```
84+
7985
## Params
8086

8187
Params must be an object, optionally containing the next parammeters:
8288

83-
### active
89+
#### active
8490

8591
Set the first element
8692

8793
active: 0
8894

89-
### delay
95+
#### delay
9096

9197
Set spin animation time
9298

9399
delay: 200
94100

95-
### auto
101+
#### auto
96102

97103
Pass an int as miliseconds to make the machine auto rotate
98104

99105
auto: false
100106

101-
### spins
107+
#### spins
102108

103109
The number of spins when auto is enabled
104110

105111
spins: false
106112

107-
### stopHidden
113+
#### stopHidden
108114

109115
Stop animation if the element is above or below the screen
110116

111117
stopHidden: true
112118

113-
### randomize
119+
#### randomize
114120

115121
Pass a function to select your own random element. This function must return an integer between 0 (first element) and max number of elements.
116122

@@ -125,6 +131,11 @@ $('#foo').slotMachine({
125131
}
126132
});
127133
```
134+
#### direction
135+
136+
Animation direction ('up' || 'down')
137+
138+
direction: 'up'
128139

129140
## Authors
130141

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jQuery-SlotMachine",
33
"description": "A simple jQuery plugin to make slot machine animation effect",
4-
"version": "2.0.11",
4+
"version": "2.1.0",
55
"keywords": [
66
"slots",
77
"gambling",

dist/jquery.slotmachine.js

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! SlotMachine - v2.0.11 - 2015-07-21
1+
/*! SlotMachine - v2.1.0 - 2015-07-21
22
* https://github.com/josex2r/jQuery-SlotMachine
33
* Copyright (c) 2015 Jose Luis Represa; Licensed MIT */
44
(function($, window, document, undefined) {
@@ -11,7 +11,8 @@
1111
spins: 5, //Number of spins when auto [int]
1212
randomize: null, //Randomize function, must return an integer with the selected position
1313
complete: null, //Callback function(result)
14-
stopHidden: true //Stops animations if the element isn´t visible on the screen
14+
stopHidden: true, //Stops animations if the element isn´t visible on the screen
15+
direction: 'up' //Animation direction ['up'||'down']
1516
};
1617

1718
var FX_FAST = 'slotMachineBlurFast',
@@ -208,8 +209,30 @@
208209
//Set min top offset
209210
this._minTop = -this._$fakeFirstTile.outerHeight();
210211

212+
//Direction parammeters
213+
this._direction = {
214+
selected: this.settings.direction === 'down' ? 'down' : 'up',
215+
up: {
216+
initial: this.getTileOffset(this.active),
217+
first: 0,
218+
last: this.getTileOffset(this.$tiles.length),
219+
to: this._maxTop,
220+
firstToLast: this.getTileOffset(this.$tiles.length),
221+
lastToFirst: 0
222+
},
223+
down: {
224+
initial: this.getTileOffset(this.active),
225+
first: this.getTileOffset(this.$tiles.length),
226+
last: 0,
227+
to: this._minTop,
228+
firstToLast: this.getTileOffset(this.$tiles.length),
229+
lastToFirst: 0
230+
},
231+
get: function(param){return this[this.selected][param];}
232+
};
233+
211234
//Show active element
212-
this.$container.css('margin-top', this.getTileOffset(this.active));
235+
this.$container.css('margin-top', this._direction.get('initial'));
213236

214237
//Start auto animation
215238
if (this.settings.auto !== false) {
@@ -222,7 +245,7 @@
222245
}
223246

224247
/**
225-
* @desc PRIVATE - Get element offset top
248+
* @desc PUBLIC - Get element offset top
226249
* @param int index - Element position
227250
* @return int - Negative offset in px
228251
*/
@@ -235,7 +258,7 @@
235258
};
236259

237260
/**
238-
* @desc PRIVATE - Get current showing element index
261+
* @desc PUBLIC - Get current showing element index
239262
* @return int - Element index
240263
*/
241264
SlotMachine.prototype.getVisibleTile = function() {
@@ -260,7 +283,7 @@
260283
};
261284

262285
/**
263-
* @desc PRIVATE - Get random element different than last shown
286+
* @desc PUBLIC - Get random element different than last shown
264287
* @param boolean cantBeTheCurrent - true||undefined if cant be choosen the current element, prevents repeat
265288
* @return int - Element index
266289
*/
@@ -275,7 +298,7 @@
275298
};
276299

277300
/**
278-
* @desc PRIVATE - Get random element based on the custom randomize function
301+
* @desc PUBLIC - Get random element based on the custom randomize function
279302
* @return int - Element index
280303
*/
281304
SlotMachine.prototype.getCustom = function() {
@@ -293,23 +316,51 @@
293316
};
294317

295318
/**
296-
* @desc PRIVATE - Get the previous element
319+
* @desc PRIVATE - Get the previous element (no direction related)
297320
* @return int - Element index
298321
*/
299-
SlotMachine.prototype.getPrev = function() {
322+
SlotMachine.prototype._getPrev = function() {
300323
var prevIndex = (this.active - 1 < 0) ? (this.$tiles.length - 1) : (this.active - 1);
301324
return prevIndex;
302325
};
303326

304327
/**
305-
* @desc PRIVATE - Get the next element
328+
* @desc PRIVATE - Get the next element (no direction related)
306329
* @return int - Element index
307330
*/
308-
SlotMachine.prototype.getNext = function() {
331+
SlotMachine.prototype._getNext = function() {
309332
var nextIndex = (this.active + 1 < this.$tiles.length) ? (this.active + 1) : 0;
310333
return nextIndex;
311334
};
312335

336+
/**
337+
* @desc PUBLIC - Get the previous element dor selected direction
338+
* @return int - Element index
339+
*/
340+
SlotMachine.prototype.getPrev = function() {
341+
return this._direction.selected === 'up' ? this._getPrev() : this._getNext();
342+
};
343+
344+
/**
345+
* @desc PUBLIC - Get the next element
346+
* @return int - Element index
347+
*/
348+
SlotMachine.prototype.getNext = function() {
349+
return this._direction.selected === 'up' ? this._getNext() : this._getPrev();
350+
};
351+
352+
/**
353+
* @desc PUBLIC - Set the spin direction
354+
* @return Object - Direction data
355+
*/
356+
SlotMachine.prototype.setDirection = function(direction) {
357+
if (this.isRunning) {
358+
return;
359+
}
360+
this._direction.selected = direction === 'down' ? 'down' : 'up';
361+
return this._direction[this._direction.selected];
362+
};
363+
313364
/**
314365
* @desc PRIVATE - Set CSS classes to make speed effect
315366
* @param string FX_SPEED - Element speed [FX_FAST_BLUR||FX_NORMAL_BLUR||FX_SLOW_BLUR||FX_STOP]
@@ -333,7 +384,7 @@
333384
* @desc PRIVATE - Reset active element position
334385
*/
335386
SlotMachine.prototype._resetPosition = function() {
336-
this.$container.css('margin-top', this.getTileOffset(this.active));
387+
this.$container.css('margin-top', this._direction.get('initial'));
337388
};
338389

339390
/**
@@ -371,7 +422,7 @@
371422
};
372423

373424
/**
374-
* @desc PRIVATE - Starts shuffling the elements
425+
* @desc PUBLIC - Starts shuffling the elements
375426
* @param int repeations - Number of shuffles (undefined to make infinite animation
376427
* @return int - Returns result index
377428
*/
@@ -421,10 +472,10 @@
421472
self.stop();
422473
} else {
423474
this.$container.animate({
424-
marginTop: this._maxTop
475+
marginTop: this._direction.get('to')
425476
}, delay, 'linear', function() {
426477
//Reset top position
427-
self.$container.css('margin-top', 0);
478+
self.$container.css('margin-top', self._direction.get('first'));
428479

429480
if (spins - 1 <= 0) {
430481
self.stop();
@@ -439,7 +490,7 @@
439490
};
440491

441492
/**
442-
* @desc PRIVATE - Stop shuffling the elements
493+
* @desc PUBLIC - Stop shuffling the elements
443494
* @return int - Returns result index
444495
*/
445496
SlotMachine.prototype.stop = function(showGradient) {
@@ -465,12 +516,12 @@
465516
if (this.futureActive > this.active) {
466517
//We are moving to the prev (first to last)
467518
if (this.active === 0 && this.futureActive === this.$tiles.length - 1) {
468-
this.$container.css('margin-top', this.getTileOffset(this.$tiles.length));
519+
this.$container.css('margin-top', this._direction.get('firstToLast'));
469520
}
470521
} else {
471522
//We are moving to the next (last to first)
472523
if (this.active === this.$tiles.length - 1 && this.futureActive === 0) {
473-
this.$container.css('margin-top', 0);
524+
this.$container.css('margin-top', this._direction.get('lastToFirst'));
474525
}
475526
}
476527

@@ -506,7 +557,7 @@
506557
};
507558

508559
/**
509-
* @desc PRIVATE - Start auto shufflings, animation stops each 3 repeations. Then restart animation recursively
560+
* @desc PUBLIC - Start auto shufflings, animation stops each 3 repeations. Then restart animation recursively
510561
*/
511562
SlotMachine.prototype.auto = function() {
512563
var self = this;
@@ -529,8 +580,6 @@
529580
}, this.settings.auto);
530581
};
531582

532-
533-
534583
/*
535584
* Create new plugin instance if needed and return it
536585
*/

dist/jquery.slotmachine.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-slotmachine",
3-
"version": "2.0.11",
3+
"version": "2.1.0",
44
"engines": {
55
"node": ">= 0.8.0"
66
},

slotmachine.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"winning",
1212
"machine"
1313
],
14-
"version": "2.0.11",
14+
"version": "2.1.0",
1515
"download": "https://github.com/josex2r/jQuery-SlotMachine",
1616
"homepage": "https://github.com/josex2r/jQuery-SlotMachine",
1717
"demo": "http://josex2r.github.io/jQuery-SlotMachine/",

0 commit comments

Comments
 (0)