-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslider.js
281 lines (222 loc) · 6.54 KB
/
slider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Vanilla Slider
* A simple slider made with pure Javascript
*
* @version 0.1
* @author Guilherme Santiago - http://guilherme.sexy
* @repo http://github.com/gsantiago/vanilla-slider
*/
(function (window, document) {
'use strict';
/**
* Default options
*/
var DEFAULTS = {
visibles: 1,
direction: 'horizontal',
controlPrev: '',
controlNext: '',
justify: true,
steps: 1,
// TO IMPLEMENT:
dots: '',
touch: true,
animation: 'slide',
autoPlay: 1000,
infinite: true
};
/**
* Utils
*/
var utils = {
// Returns true if 'obj' is a node element
isDOM: function (o) {
return (
typeof HTMLElement === 'object' ? o instanceof HTMLElement :
o && typeof o === 'object' && o !== null && o.nodeType === 1 && typeof o.nodeName === 'string'
);
},
// Merge two objects. Similar to $.extends from jQuery
merge: function (obj1, obj2) {
var result = {};
for (var prop in obj1) {
if (obj2.hasOwnProperty(prop)) {
result[prop] = obj2[prop];
} else {
result[prop] = obj1[prop];
}
}
return result;
},
// A simple iterator for collections
each: function (group, callback) {
for (var i = 0, max = group.length; i < max; i += 1) {
callback.call(group[i], i);
}
}
}
/**
* Constructor for the slider
*
* @class Slider
* @constructor
* @param {String | Node Element} Selector for the slider's container
* @param {Object} Options
*/
function Slider(element, options) {
if (!(this instanceof Slider)) return new Slider(element, options);
if (utils.isDOM(element)) {
this.container = element;
} else {
this.container = document.getElementById(element);
}
this.settings = utils.merge(DEFAULTS, options);
this.slider = this.container.children[0];
this.items = this.slider.children;
this.containerWidth = this.container.offsetWidth;
this.containerHeight = this.container.offsetHeight;
this.itemWidth = this.items[0].offsetWidth;
this.itemHeight = this.items[0].offsetHeight;
this.itemMargin = 0;
if (this.settings.direction === 'vertical') {
this.containerDimension = this.containerHeight;
this.itemDimension = this.itemHeight;
} else {
this.containerDimension = this.containerWidth;
this.itemDimension = this.itemWidth;
}
// Justify the margins acording to the number of items to show (visibles)
if (this.settings.visibles > 1 && this.settings.justify) {
this.justifyItems();
}
if (this.settings.controlNext || this.settings.controlPrev) {
this.addControls(this.settings.controlNext, this.settings.controlPrev, this.settings.steps);
}
}
// An alias for prototype
Slider.fn = Slider.prototype;
/*
* Justify the margins between the items acording to the number
* of items to show (the visibles), and the direction (vertical or horizontal)
*
* @method justifyItems
*/
Slider.fn.justifyItems = function () {
var visibles = this.settings.visibles,
items = this.items,
direction = this.settings.direction,
margin;
margin = (this.containerDimension - (this.itemDimension * visibles)) / (visibles - 1);
margin = Math.ceil(margin);
this.itemMargin = margin;
utils.each(items, function () {
this.style[direction === 'vertical' ? 'marginBottom' : 'marginRight'] = margin + 'px';
});
};
/**
* Static method to return the max or min position for the Slider
*
* @method getLimit
* @param {Object} The instance of slider
* @param {String} The position ('max' or 'min')
*/
Slider.getLimit = function (instance, pos) {
var settings = instance.settings,
direction = settings.direction,
itemDimension = instance.itemDimension,
itemMargin = instance.itemMargin,
visibles = settings.visibles,
items = instance.items;
if (direction === 'vertical' && pos === 'max') {
return 0;
}
if (direction === 'vertical' && pos === 'min') {
return (((itemDimension + itemMargin) * (items.length - visibles + 1)) - itemMargin) * -1;
}
if (direction === 'horizontal' && pos === 'max') {
return ((itemDimension + itemMargin) * (items.length - visibles + 1)) - itemMargin;
}
if (direction === 'horizontal' && pos === 'min') {
return 0;
}
};
/**
* Check if the Slider is at its limit
*
* @method isAtLimit
*/
Slider.fn.isAtLimit = function (nextPos) {
var max = Slider.getLimit(this, 'max'),
min = Slider.getLimit(this, 'min');
if (nextPos < min) {
return true;
}
if (nextPos > max) {
return true;
}
return false;
};
/**
* Return the next position for the slider based in the number of steps
*
* @method getNextPos
*/
Slider.fn.getNextPos = function (steps) {
var currentPos,
direction,
nextPos;
direction = this.settings.direction;
currentPos = this.slider.style[direction === 'vertical' ? 'top' : 'right'];
if (currentPos) {
currentPos = Math.floor(parseInt(currentPos));
}
if (direction === 'vertical') {
nextPos = currentPos - (((this.itemDimension + this.itemMargin)) * steps);
} else {
nextPos = currentPos + (((this.itemDimension + this.itemMargin)) * steps)
}
return Math.ceil(nextPos);
};
/**
* Move the slider.
* Pass a negative number to go in the inverse direction
*
* @method move
* @param {Integer} Number of items to move
*/
Slider.fn.move = function (steps) {
steps = steps || 1;
var nextPos = this.getNextPos(steps),
direction = this.settings.direction;
if (this.isAtLimit(nextPos)) {
return this;
}
this.slider.style[direction === 'vertical' ? 'top' : 'right'] = nextPos + 'px';
};
/**
* Add event listeners for the controls passed
*
* @method addControls
* @param {String | Node Element} Next Control
* @param {String | Node Element} Prev Control
* @param {Integer} Number of items (steps) to avance/return
*/
Slider.fn.addControls = function (next, prev, steps) {
var that = this;
if (!utils.isDOM(next)) {
next = document.getElementById(next);
}
if (!utils.isDOM(prev)) {
prev = document.getElementById(prev);
}
next.addEventListener('click', function (e) {
e.preventDefault();
that.move(steps);
});
prev.addEventListener('click', function (e) {
e.preventDefault();
that.move(steps * -1);
});
};
window.Slider = Slider;
}(window, document));