forked from thebird/Swipe
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbaconswipe.js
More file actions
375 lines (297 loc) · 12.7 KB
/
baconswipe.js
File metadata and controls
375 lines (297 loc) · 12.7 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* BaconSwipe 1.0
* Adapted from:
* Swipe 1.0
* Brad Birdsall, Prime
* Copyright 2011, Licensed GPL & MIT
*
*/
window.BaconSwipe = function(element, options, cellPaddedWidth, cellPaddedHeight, columns) {
// return immediately if element doesn't exist
if (!element) return null;
var _this = this;
///////
//OPTIONS
////
this.options = options || {};
this.index = this.options.startSlide || 0;
this.vindex = this.options.vindex || 0;
this.speed = this.options.speed || 300;
this.callback = this.options.callback || function() {};
this.delay = this.options.auto || 0;
this.individual = this.options.individual || false;
this.scrollsVertically = this.options.scrollsVertically == null ? true : this.options.scrollsVertically;
this.columns = columns;
// reference dom elements
this.container = element;
this.element = this.container.children[0]; // the slide pane/<ul>
// static css
this.container.style.overflow = 'hidden';
this.element.style.listStyle = 'none';
this.element.style.margin = 0;
// trigger slider initialization
this.setup(cellPaddedWidth, cellPaddedHeight);
// begin auto slideshow
this.begin();
// add event listeners
if (this.element.addEventListener) {
this.element.addEventListener('touchstart', this, false);
this.element.addEventListener('touchmove', this, false);
this.element.addEventListener('touchend', this, false);
this.element.addEventListener('webkitTransitionEnd', this, false);
this.element.addEventListener('msTransitionEnd', this, false);
this.element.addEventListener('oTransitionEnd', this, false);
this.element.addEventListener('transitionend', this, false);
}
};
BaconSwipe.prototype = {
setup: function(cellwidth, cellheight) {
// get and measure amt of slides
this.slides = this.element.children;
this.length = this.slides.length;
// return immediately if their are less than two slides
if (this.length < 2) return null;
// determine width of each slide
this.width = ("getBoundingClientRect" in this.container) ? this.container.getBoundingClientRect().width : this.container.offsetWidth;
this.height = ("getBoundingClientRect" in this.container) ? this.container.getBoundingClientRect().height : this.container.offsetHeight;
// return immediately if measurement fails
if (!this.width || !this.height) return null;
// hide slider element but keep positioning during setup
this.container.style.visibility = 'hidden';
// set sizing attributes
var index = this.slides.length;
var extrawidth = this.width%cellwidth;
var cellsperrow = Math.floor(this.width/cellwidth);
var marginRight = extrawidth/cellsperrow;
//define the horizontal size
this.cellhorizontalsize = marginRight + cellwidth + 1;
//set the UL width
this.element.style.width = this.columns*(this.cellhorizontalsize)+'px';
//calculate heights
var rows = 0;
var extraheight = this.height;
while(extraheight > 0) {
rows++;
extraheight -= cellheight;
}
extraheight += cellheight;
var marginBottom = extraheight/(rows - 1);
this.cellverticalsize = cellheight + marginBottom;
//set the number of "pages" the slider will slide through depending on if a "page" is an individual grid item or a full viewport's worth of grid items
if(this.individual) {
this.hpages = this.columns - 1;
this.vpages = rows;
}
else {
this.hpages = Math.ceil(this.columns/(this.width/cellwidth));
this.vpages = Math.ceil(rows/(this.height/cellheight));
}
//set sizing variables for each cell
while (index--) {
var el = this.slides[index];
el.style.width = cellwidth+ 'px';
el.style.height = cellheight+'px';
el.style.marginRight = marginRight+'px';
el.style.marginBottom = marginBottom +'px';
el.style.display = 'inline-block';
el.style.verticalAlign = 'top';
}
this.slides[this.slides.length - 1].style.borderRight = 'none';
this.element.style.webkitTransform = 'translate(0, 0)';
// set start position and force translate to remove initial flickering
this.slide(this.index, 0);
this.vslide(this.vindex, 0);
// show slider element
this.container.style.visibility = 'visible';
},
slide: function(index, duration) {
var style = this.element.style;
// fallback to default speed
if (duration == undefined) {
duration = this.speed;
}
// set duration speed (0 represents 1-to-1 scrolling)
style.webkitTransitionDuration = style.MozTransitionDuration = style.msTransitionDuration = style.OTransitionDuration = style.transitionDuration = duration + 'ms';
// translate to given index position
/*if(this.individual) {
style.MozTransform = style.webkitTransform = 'translate3d(' + -(index * this.cellhorizontalsize) + 'px,0,0)';
style.msTransform = style.OTransform = 'translateX(' + -(index * this.cellhorizontalsize) + 'px)';
} else {
style.MozTransform = style.webkitTransform = 'translate3d(' + -(index * this.width) + 'px,0,0)';
style.msTransform = style.OTransform = 'translateX(' + -(index * this.width) + 'px)';
}*/
if(this.individual) {
style.MozTransform = style.webkitTransform = 'translate3d(' + -(index * this.cellhorizontalsize) + 'px,' + -(this.vindex * this.cellverticalsize) + 'px,0)';
style.msTransform = style.OTransform = 'translateX(' + -(index * this.cellhorizontalsize) + 'px)';
} else {
style.MozTransform = style.webkitTransform = 'translate3d(' + -(index * this.width) + 'px,' + -(this.vindex * this.height) + 'px,0)';
style.msTransform = style.OTransform = 'translateX(' + -(index * this.width) + 'px)';
}
// set new index to allow for expression arguments
this.index = index;
},
vslide: function(vindex, duration) {
var style = this.element.style;
if (duration == undefined)
duration = this.speed;
style.webkitTransitionDuration = style.MozTransitionDuration = style.msTransitionDuration = style.OTransitionDuration = style.transitionDuration = duration + 'ms';
/*if(this.individual) {
style.MozTransform = style.webkitTransform = 'translate3d(0,' + -(vindex * this.cellverticalsize) + 'px,0)';
style.msTransform = style.OTransform = 'translateY(' + -(vindex * this.cellverticalsize) + 'px)';
} else {
style.MozTransform = style.webkitTransform = 'translate3d(0,' + -(vindex * this.height) + 'px,0)';
style.msTransform = style.OTransform = 'translateY(' + -(vindex * this.height) + 'px)';
}*/
if(this.individual) {
style.MozTransform = style.webkitTransform = 'translate3d(' + -(this.index * this.cellhorizontalsize) + 'px,' + -(vindex * this.cellverticalsize) + 'px,0)';
style.msTransform = style.OTransform = 'translateY(' + -(vindex * this.cellverticalsize) + 'px)';
} else {
style.MozTransform = style.webkitTransform = 'translate3d(' + -(this.index * this.width) + 'px,' + -(vindex * this.height) + 'px,0)';
style.msTransform = style.OTransform = 'translateY(' + -(vindex * this.height) + 'px)';
}
this.vindex = vindex;
},
getPos: function() {
// return current index position
return this.index;
},
prev: function(delay) {
// cancel next scheduled automatic transition, if any
this.delay = delay || 0;
clearTimeout(this.interval);
// if not at first slide
if (this.index) this.slide(this.index-1, this.speed);
},
next: function(delay) {
// cancel next scheduled automatic transition, if any
this.delay = delay || 0;
clearTimeout(this.interval);
if (this.index < this.hpages - 1) this.slide(this.index+1, this.speed); // if not last slide
else this.slide(0, this.speed); //if last slide return to start
},
begin: function() {
var _this = this;
this.interval = (this.delay)
? setTimeout(function() {
_this.next(_this.delay);
}, this.delay)
: 0;
},
stop: function() {
this.delay = 0;
clearTimeout(this.interval);
},
resume: function() {
this.delay = this.options.auto || 0;
this.begin();
},
handleEvent: function(e) {
switch (e.type) {
case 'touchstart': this.onTouchStart(e); break;
case 'touchmove': this.onTouchMove(e); break;
case 'touchend': this.onTouchEnd(e); break;
case 'webkitTransitionEnd':
case 'msTransitionEnd':
case 'oTransitionEnd':
case 'transitionend': this.transitionEnd(e); break;
}
},
transitionEnd: function(e) {
if (this.delay) this.begin();
this.callback(e, this.index, this.slides[this.index]);
},
onTouchStart: function(e) {
// prevent native scrolling
e.preventDefault();
this.start = {
// get touch coordinates for delta calculations in onTouchMove
pageX: e.touches[0].pageX,
pageY: e.touches[0].pageY,
// set initial timestamp of touch sequence
time: Number( new Date() )
};
// used for testing first onTouchMove event
this.isScrolling = undefined;
// reset deltaX and Y
this.deltaX = 0;
this.deltaY = 0;
// set transition time to 0 for 1-to-1 touch movement
this.element.style.MozTransitionDuration = this.element.style.webkitTransitionDuration = 0;
this.matrix = new WebKitCSSMatrix(window.getComputedStyle(this.element).webkitTransform);
e.stopPropagation();
},
onTouchMove: function(e) {
// ensure swiping with one touch and not pinching
if(e.touches.length > 1 || e.scale && e.scale !== 1) return;
this.deltaX = e.touches[0].pageX - this.start.pageX;
this.deltaY = e.touches[0].pageY - this.start.pageY;
// determine if scrolling test has run - one time test
if ( typeof this.isScrolling == 'undefined') {
this.isScrolling = !!( this.isScrolling || Math.abs(this.deltaX) < Math.abs(this.deltaY) );
}
//increase resistance if first or last slide
this.deltaX = this.deltaX/
( (!this.index && this.deltaX > 0 //if on first item sliding right
|| this.index == this.hpages - 1 && this.deltaX < 0 ) //or on last item sliding left
? (Math.abs(this.deltaX)/this.cellhorizontalsize + 1) //decrease deltaX proportionately
: 1 ); //otherwise leave it alone
this.deltaY = this.deltaY/
( (!this.vindex && this.deltaY > 0 //if on first row sliding down
|| this.index == this.vpages - 1 && this.deltaY < 0 ) //or on bottom row sliding up
? (Math.abs(this.deltaY)/this.cellverticalsize + 1)
: 1);
if (this.individual && this.isScrolling && this.scrollsVertically) {
this.element.style.webkitTransform = this.matrix.translate(0,this.deltaY);
} else if (this.isScrolling && this.scrollsVertically) {
this.element.style.webkitTransform = this.matrix.translate(0,this.deltaY);
} else if (this.individual) {
this.element.style.webkitTransform = this.matrix.translate(this.deltaX, 0);
} else{
this.element.style.webkitTransform = this.matrix.translate(this.deltaX,0);
}
e.stopPropagation();
},
onTouchEnd: function(e) {
// determine if slide attempt triggers next/prev slide
var isValidSlide;
// determine if slide attempt is past start and end
var isPastBounds;
var indexchange = 0;
// if not scrolling vertically
if (!this.isScrolling) {
isValidSlide = (Math.abs(this.deltaX) > 20) || (Math.abs(this.deltaX) > this.cellhorizontalsize/2);
isPastBounds = (!this.index && this.deltaX > 0) || (this.index == this.hpages - 1 && this.deltaX < 0);
if (isValidSlide && !isPastBounds){
if (!this.individual) {
indexchange = (this.deltaX < 0 ? 1: -1);
}
else {
indexchange = -Math.floor(this.deltaX/this.cellhorizontalsize);
if ((this.index + indexchange) > this.hpages - 1)
indexchange = this.hpages - 1 - this.index;
else if (this.index + indexchange < 0)
indexchange = -this.index;
}
}
// call slide function with slide end value based on isValidSlide and isPastBounds tests
this.slide( this.index + indexchange, this.speed );
}
else if (this.isScrolling && this.scrollsVertically) {
isValidSlide = (Math.abs(this.deltaY) > 20) || (Math.abs(this.deltaY) > this.cellverticalsize/2);
isPastBounds = (!this.vindex && this.deltaY > 0) || (this.vindex == this.vpages -1 && this.deltaY < 0);
if (!this.individual) {
indexchange = (this.deltaY < 0 ? 1: -1);
}
else {
indexchange = -Math.floor(this.deltaX/this.cellverticalsize);
if ((this.vindex + indexchange) > this.vpages - 1)
indexchange = this.vpages - 1 - this.vindex;
else if (this.vindex + indexchange < 0)
indexchange = -this.vindex;
}
// call slide function with slide end value based on isValidSlide and isPastBounds tests
this.vslide( this.vindex + (isValidSlide && !isPastBounds ? (this.deltaY < 0 ? 1 : -1) : 0 ), this.speed );
}
e.stopPropagation();
}
};