|
1 | 1 | # jQuery-SlotMachine [](https://travis-ci.org/josex2r/jQuery-SlotMachine) [](https://david-dm.org/josex2r/jQuery-SlotMachine) [](https://david-dm.org/josex2r/jQuery-SlotMachine#info=devDependencies) |
2 | 2 |
|
3 | | -A simple, lightweight jQuery plugin to make slot machine animation effect. |
| 3 | +> :mega: jQuery is not neccessary now! The name it's just legacy. |
4 | 4 |
|
5 | | -[Check the example page!](http://josex2r.github.io/jQuery-SlotMachine/) |
| 5 | +A simple, and lightweight piece of code to make slot machine animation effect. |
| 6 | +It also exports a js wrapper to allow the usage with jQuery. |
| 7 | + |
| 8 | +To preview what you can do [check the example page!](http://josex2r.github.io/jQuery-SlotMachine/) |
6 | 9 |
|
7 | 10 | ## Installation |
8 | 11 |
|
9 | | -Install the component using [Bower](http://bower.io/): |
| 12 | +Install the component using [npm](https://www.npmjs.com/package/jquery-slotmachine): |
10 | 13 |
|
11 | | -```sh |
12 | | -$ bower install jquery-slotmachine --save |
| 14 | +```bash |
| 15 | +npm install jquery-slotmachine --save |
13 | 16 | ``` |
14 | 17 |
|
15 | | -Include the script located in *dist* folder *after* the jQuery library: |
| 18 | +Install the component using [Bower](http://bower.io/): |
16 | 19 |
|
17 | | -```html |
18 | | -<script src="/path/to/jquery.slotmachine.min.js"></script> |
| 20 | +```bash |
| 21 | +bower install jquery-slotmachine --save |
19 | 22 | ``` |
20 | 23 |
|
21 | | -## Usage |
22 | | - |
23 | | -Creating the machine: |
| 24 | +## Example |
24 | 25 |
|
25 | | -```javascript |
26 | | -var machine = $(foo).slotMachine( params ); |
| 26 | +```html |
| 27 | +<div id="machine"> |
| 28 | + <div>Madrid</div> |
| 29 | + <div>London</div> |
| 30 | + <div>New York</div> |
| 31 | +</div> |
| 32 | + |
| 33 | +<script> |
| 34 | +const el = document.querySelector('#machine'); |
| 35 | +const machine = new SlotMachine(el, { |
| 36 | + active: 1, |
| 37 | + delay: 450, |
| 38 | + auto: 1500 |
| 39 | +}); |
| 40 | +</script> |
27 | 41 | ``` |
28 | 42 |
|
29 | | -Get machine instance: |
| 43 | +> Lookup the sourcecode in the [examples page](http://josex2r.github.io/jQuery-SlotMachine/) to see more examples. |
30 | 44 |
|
31 | | -```javascript |
32 | | -var machine = $(foo).slotMachine(); |
33 | | -``` |
| 45 | +## Usage |
34 | 46 |
|
35 | | -Shuffle: |
| 47 | +Include the script located in *dist* folder: |
36 | 48 |
|
37 | | -```javascript |
38 | | -machine.shuffle( repeat, onStopCallback ); //No args to make rotate infinitely, `repeat` is optional |
| 49 | +```html |
| 50 | +<script src="/path/to/slotmachine.min.js"></script> |
39 | 51 | ``` |
40 | 52 |
|
41 | | -Change the selected element: |
| 53 | +Then you can make it work calling the lib in your app: |
42 | 54 |
|
43 | 55 | ```javascript |
44 | | -machine.prev(); //Previous element |
45 | | - |
46 | | -machine.next(); //Next element |
| 56 | +const element = document.getElementById('my-machine'); |
| 57 | +const machine = new SlotMachine(element, { /* options */ }); |
47 | 58 | ``` |
48 | 59 |
|
49 | | -Stop the machine: |
| 60 | +If you preffer jQuery style then import the wrapper *after* the jQuery library: |
50 | 61 |
|
51 | | -```javascript |
52 | | -machine.stop(); |
| 62 | +```html |
| 63 | +<script src="/path/to/jquery.min.js"></script> |
| 64 | +<script src="/path/to/jslotmachine.min.js"></script> |
| 65 | +<script src="/path/to/jquery.slotmachine.min.js"></script> |
53 | 66 | ``` |
54 | 67 |
|
55 | | -Get selected element: |
56 | | - |
57 | 68 | ```javascript |
58 | | -machine.active; //Current element index |
| 69 | +$(document).ready(function(){ |
| 70 | + $('#my-machine').slotMachine({ /* options */ }); |
| 71 | +}); |
59 | 72 | ``` |
60 | 73 |
|
61 | | -Get the selected element if shuffling: |
| 74 | +### Settings |
62 | 75 |
|
63 | | -```javascript |
64 | | -machine.futureActive; //Future active element index |
65 | | -``` |
66 | | - |
67 | | -Check if the machine is running: |
| 76 | +Use the first argument of the function to pass an object with the options: |
68 | 77 |
|
69 | 78 | ```javascript |
70 | | -machine.running; //Returns boolean |
| 79 | +const machine = new SlotMachine(element, { |
| 80 | + active: 2, |
| 81 | + auto: true |
| 82 | +}); |
71 | 83 | ``` |
72 | 84 |
|
73 | | -Check if the machine is stopping: |
| 85 | +| Name | Type | Default | Description | |
| 86 | +|----------------|------------|---------------|-------------------------------------------------------------------------------------| |
| 87 | +| **active** | `Number` | `0` | The initial visible element (0 means the first one) | |
| 88 | +| **delay** | `Number` | `200` | Duration (in ms) of each spin | |
| 89 | +| **auto** | `Boolean` | `false` | Runs the carousel mode when creating the machine | |
| 90 | +| **spins** | `Number` | `5` | Number of spins after stop in carousel mode | |
| 91 | +| **randomize** | `Function` | `null` | Function (returns number) that is going to be called to set the next active element | |
| 92 | +| **onComplete** | `Function` | `null` | Callback after each spin in carousel mode | |
| 93 | +| **inViewport** | `Boolean` | `true` | Only spin when the machine is inside the viewport | |
| 94 | +| **direction** | `String` | `up` | The spin direction (possible values are `up` and `down`) | |
| 95 | +| **transition** | `String` | `ease-in-out` | The CSS transition | |
| 96 | + |
| 97 | +### Properties |
| 98 | + |
| 99 | +- `machine.nextActive`: Get the next active element (only while shuffling). |
| 100 | +- `machine.nextIndex`: Next element index according to the current direction. |
| 101 | +- `machine.prevIndex`: Prev element index according to the current direction. |
| 102 | +- `machine.random`: Get rando index between the machine bounds. |
| 103 | +- `machine.running`: Check if the machine is running. |
| 104 | +- `machine.stopping`: Check if the machine is stopping. |
| 105 | +- `machine.visible`: Check if the machine is visible. |
| 106 | +- `machine.visibleTile`: Get the current visible element in the machine viewport. |
| 107 | +- `machine.active`: Alias to the `active` setting. |
| 108 | +- `machine.randomize`: Alias to the `randomize` setting. |
| 109 | +- `machine.direction`: Alias to the `direction` setting. |
| 110 | +- `machine.transition`: Alias to the `transition` setting. |
| 111 | + |
| 112 | +### Methods |
| 113 | + |
| 114 | +`machine.shuffle(spins, callback)`: Starts spining the machine. |
| 115 | + - spins (`Number`): Optionally set the number of spins. |
| 116 | + - callback(`Function`): Callback triggered when the machine stops. |
74 | 117 |
|
75 | 118 | ```javascript |
76 | | -machine.stopping; //Returns boolean |
| 119 | +// Do a single spin |
| 120 | +machine.shuffle(); |
| 121 | +// Do a single spin and then shows an alert |
| 122 | +machine.shuffle(() => alert('Stop!')); |
| 123 | +// Do 5 spins before stop |
| 124 | +machine.shuffle(5); |
| 125 | +// Do 7 spins and then showing an alert |
| 126 | +machine.shuffle(7, () => alert('Stop!')); |
| 127 | +// "Infinite" spins |
| 128 | +machine.shuffle(9999999); // O_O |
77 | 129 | ``` |
78 | 130 |
|
79 | | -Check if the machine is visible: |
80 | | - |
81 | | -```javascript |
82 | | -machine.visible; //Returns boolean |
83 | | -``` |
| 131 | +`machine.stop(callback)`: Manually stops the machine. |
| 132 | + - callback(`Function`): Callback triggered when the machine stops. |
84 | 133 |
|
85 | | -Change spin result, if the returned value is out of bounds, the element will be randomly choosen: |
| 134 | +For example, start spinning the machine and stop it after pressing a button: |
86 | 135 |
|
87 | 136 | ```javascript |
88 | | -machine.randomize = foo; //foo must be a function (should return int) or an int |
| 137 | +machine.shuffle(99999); |
| 138 | +// Add the button listener |
| 139 | +myButton.addEventListener('click', () => { |
| 140 | + // Stop spinning |
| 141 | + machine.stop(); |
| 142 | +}); |
89 | 143 | ``` |
90 | 144 |
|
91 | | -Change spin direction, machine must not be running: |
| 145 | +`machine.next()`/`machine.prev()`: Spin to the next/previous element. |
92 | 146 |
|
93 | 147 | ```javascript |
94 | | -machine.direction = direction; //direction must be a String ('up' || 'down') |
| 148 | +// Spin to the previous element |
| 149 | +machine.prev(); |
| 150 | +// Spin to the next element |
| 151 | +machine.next(); |
95 | 152 | ``` |
96 | 153 |
|
97 | | -Destroy the machine. It will be useful when you want to reuse DOM: |
| 154 | +`machine.run()`: Starts the preview mode, it will spin/stop given a delay (more info in options). |
98 | 155 |
|
99 | 156 | ```javascript |
100 | | -machine.destroy(); |
| 157 | +machine.run(); |
101 | 158 | ``` |
102 | 159 |
|
103 | | -## Params |
104 | | - |
105 | | -Params must be an object, optionally containing the next parammeters: |
106 | | - |
107 | | -#### active |
108 | | - |
109 | | -Set the first element |
110 | | - |
111 | | - active: 0 |
112 | | - |
113 | | -#### delay |
114 | | - |
115 | | -Set spin animation time |
116 | | - |
117 | | - delay: 200 |
118 | | - |
119 | | -#### auto |
120 | | - |
121 | | -Pass an int as miliseconds to make the machine auto rotate |
122 | | - |
123 | | - auto: false |
124 | | - |
125 | | -#### spins |
126 | | - |
127 | | -The number of spins when auto is enabled |
128 | | - |
129 | | - spins: false |
130 | | - |
131 | | -#### stopHidden |
132 | | - |
133 | | -Stop animation if the element is above or below the screen |
134 | | - |
135 | | - stopHidden: true |
136 | | - |
137 | | -#### randomize |
138 | | - |
139 | | -Pass a function to select your own random element. This function must return an integer between 0 (first element) and max number of elements. |
140 | | - |
141 | | - randomize: function(activeElementIndex){} //activeElementIndex = current selected index |
142 | | - |
143 | | -Example (this machine always shows first element): |
| 160 | +`machine.run()`: Destroys the machine. It will be useful when you want to reuse DOM. |
144 | 161 |
|
145 | 162 | ```javascript |
146 | | -$('#foo').slotMachine({ |
147 | | - randomize : function(activeElementIndex){ |
148 | | - return 0; |
149 | | - } |
150 | | -}); |
| 163 | +machine.destroy(); |
151 | 164 | ``` |
152 | | -#### direction |
153 | | - |
154 | | -Animation direction ('up' || 'down') |
155 | | - |
156 | | - direction: 'up' |
157 | 165 |
|
158 | 166 | ## Authors |
159 | 167 |
|
|
0 commit comments