-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslideToucher.js
364 lines (282 loc) · 14.1 KB
/
slideToucher.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
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
/*********
The MIT License (MIT)
Copyright (c) 2013 Jurijs Petusko | Yuri Petusko | [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Credits:
Plugin is loosely based on tutorial by Martin Kool https://twitter.com/mrtnkl
http://mobile.smashingmagazine.com/2012/06/21/play-with-hardware-accelerated-css/
Very clever way iScroll plugin works out your vendor prefix.
Credits to Matteo Spinelli, http://cubiq.org
************/
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
var currentSlide;
var postInView;
var plugin;
var colorsArray;
var currentColor;
;(function ($, window, document, undefined) {
function slideToucher(el, options) {
/*
Defiantly too many variables here. Need to find a way to reduce 'em
*/
plugin = this;
plugin.el = el;
plugin.$el = $(el);
var sliding = startClientX = startClientY = startPixelOffset = 0;
/*
should I come up with better name for this var?
*/
var cachedElStyle = plugin.el.style;
/*
Should I use DOM querySelectorAll? All mobile phone browsers should be capable of it now...
*/
var $slide = plugin.$el.find('.slide');
var $row = plugin.$el.find('.savedPostWrapper');
var slideWidth = "";
var slideHeight = "";
var downX = "";
var upX = "";
var downXtime = "";
var upXtime = "";
var downY = "";
var upY = "";
var downYtime = "";
var upYtime = "";
var slideType = "";
var offsetLeft = "";
var offsetTop = "";
var defaults = {
/* Anyone in here? */
}
plugin.init = function () {
plugin.options = $.extend({
vertical: false,
horizontal: true,
sliderWidth: '100%',
slideTreshold: 15
}, options);
plugin.defineVendorPrefix();
plugin.setWidth();
plugin.recordDimensions();
plugin.bindEvents();
/*
Need to come up with less expensive selectors.
*/
$row.filter(".current-row").removeClass("current-row");
$slide.filter(".current").removeClass("current");
$row.eq(plugin.vertical.currentSlide).addClass("current-row").find(".slide").eq(plugin.horizontal.currentSlide).addClass("current");
};
plugin.getTransitionTime = function (downPos, upPos, downPosTime, upPosTime, dir) {
/*
calculates the transition time from the speed of the swipe.
THIS IS BAD. I DON'T THINK THAT I KNOW WHAT i'M DOIJNG HERE. NEED TO LOOK INTO IT.
*/
var distance_delta = Math.abs(downPos - upPos);
var time_delta = upPosTime - downPosTime;
var transition_time = 1 / (distance_delta / time_delta);
//Let's make transition speed based on screen size, giving 1024x768 screen a max 1ms speed
var screenSizeDelta = dir === "Y" ? slideHeight / 768 : sliderWidth / 1024;
return Math.max(Math.min(transition_time * screenSizeDelta, screenSizeDelta) / 2, 0.1);
};
plugin.defineVendorPrefix = function() {
/*
This is very clever way iScroll plugin works out your vendor prefix.
Credits to Matteo Spinelli, http://cubiq.org
*/
var dummyStyle = document.createElement('div').style;
plugin.vendor = (function () {
var vendors = 't,webkitT,MozT,msT,OT'.split(','),
t,
i = 0,
l = vendors.length;
for ( ; i < l; i++ ) {
t = vendors[i] + 'ransform';
if ( t in dummyStyle ) {
return vendors[i].substr(0, vendors[i].length - 1);
}
}
return false;
})();
plugin.cssVendor = plugin.vendor ? '-' + plugin.vendor.toLowerCase() + '-' : '';
// Style properties
plugin.transform = plugin.prefixStyle('transform');
plugin.transitionDuration = plugin.prefixStyle('transitionDuration');
plugin.transitionEnd = plugin.prefixStyle('TransitionEnd');
};
plugin.prefixStyle = function(style) {
if ( plugin.vendor === '' ) return style;
style = style.charAt(0).toUpperCase() + style.substr(1);
return plugin.vendor + style;
};
plugin.setWidth = function () {
/*
Setting with of parent container to number of slides in first row * first slide width.
This will not work very well if other rows has different number of slides.
*/
plugin.$el.css("width", plugin.options.sliderWidth * $row.eq(0).find(".slide").length);
$slide.css("width", slideWidth);
};
plugin.recordDimensions = function(){
sliderWidth = plugin.options.sliderWidth;
slideHeight = $row.height();
plugin.vertical = {
slideCount: $row.length,
slideSize: slideHeight,
currentSlide: Math.round(Math.abs(plugin.$el.position().top / slideHeight))
}
plugin.horizontal = {
slideCount: $row.eq(0).find('.slide').length,
slideSize: sliderWidth,
currentSlide: Math.round(Math.abs(plugin.$el.position().left / sliderWidth))
}
plugin.vertical.pixelOffset = plugin.vertical.currentSlide * -slideHeight;
plugin.horizontal.pixelOffset = plugin.horizontal.currentSlide * -sliderWidth;
};
plugin.bindEvents = function () {
plugin.$el.on('touchstart', plugin.slideStart);
plugin.$el.on('touchmove', plugin.slide);
plugin.$el.on('touchend', plugin.slideEnd);
plugin.$el.bind(plugin.transitionEnd, function (event) {
event.stopPropagation();
if (event.target !== $(this)[0]) return;
/*
Need to come up with less expensive selectors.
*/
$row.filter(".current-row").removeClass("current-row");
$row.eq(plugin.vertical.currentSlide).addClass("current-row");
$slide.filter(".current").removeClass("current");
$row.filter(".current-row").find(".slide").eq(plugin.horizontal.currentSlide).addClass("current");
plugin.$el.trigger("slideToucherTransitionCompleted");
});
plugin.$el.trigger(plugin.transitionEnd);
};
plugin.slideStart = function (event) {
/*
Register position on touch start
*/
if (event.originalEvent.touches) {
cachedElStyle[plugin.transitionDuration] = "";
downX = parseInt(event.originalEvent.touches[0].pageX, 10);
upX = downX;
downXtime = new Date().getTime();
downY = parseInt(event.originalEvent.touches[0].pageY, 10);
upY = downY;
downYtime = new Date().getTime();
event = event.originalEvent.touches[0];
}
if (sliding == 0) {
sliding = 1;
offsetLeft = plugin.$el.position().left;
offsetTop = plugin.$el.position().top;
startClientX = event.clientX;
startClientY = event.clientY;
}
};
plugin.slide = function (event) {
/*
Drag slide on touchmove
*/
event.preventDefault();
if (event.originalEvent.touches) {
upX = event.originalEvent.touches[0].pageX;
upY = event.originalEvent.touches[0].pageY;
upXtime = upYtime = new Date().getTime();
event = event.originalEvent.touches[0];
}
var deltaSlideX = event.clientX - startClientX;
var deltaSlideY = event.clientY - startClientY;
if (sliding == 1 && deltaSlideX != 0 && Math.abs(deltaSlideX) > plugin.options.slideTreshold && plugin.options.horizontal) {
sliding = 2;
slideType = "horizontal";
startPixelOffset = plugin.horizontal.pixelOffset;
} else if (sliding == 1 && deltaSlideY != 0 && Math.abs(deltaSlideY) > plugin.options.slideTreshold && plugin.options.vertical) {
sliding = 2;
slideType = "vertical";
startPixelOffset = plugin.vertical.pixelOffset;
}
if (slideType === "horizontal") {
var deltaSlide = deltaSlideX,
startClienPos = startClientX,
eventPos = event.clientX;
} else if (slideType === "vertical") {
var deltaSlide = deltaSlideY,
startClienPos = startClientY,
eventPos = event.clientY;
}
if (sliding == 2) {
var touchPixelRatio = 1;
if ((plugin[slideType].currentSlide == 0 && eventPos > startClienPos) || (plugin[slideType].currentSlide == plugin[slideType].slideCount - 1 && eventPos < startClienPos)) {
touchPixelRatio = 3;
}
plugin[slideType].pixelOffset = startPixelOffset + deltaSlide / touchPixelRatio;
if (slideType === "horizontal") {
cachedElStyle[plugin.transform] = 'translate3d(' + plugin[slideType].pixelOffset + 'px, ' + offsetTop + 'px, 0)';
} else if (slideType === "vertical") {
cachedElStyle
}
}
};
//var postInView = -1;
plugin.slideEnd = function (event) {
/*
Finish slide transition on touchend
*/
if (sliding == 2) {
sliding = 0;
plugin[slideType].currentSlide = plugin[slideType].pixelOffset < startPixelOffset ? plugin[slideType].currentSlide + 1 : plugin[slideType].currentSlide - 1;
plugin[slideType].currentSlide = Math.min(Math.max(plugin[slideType].currentSlide, 0), plugin[slideType].slideCount - 1);
plugin[slideType].pixelOffset = plugin[slideType].currentSlide * -plugin[slideType].slideSize;
if (slideType === "horizontal") {
var transitionTime = plugin.getTransitionTime(downX, upX, downXtime, upXtime, "X");
cachedElStyle[plugin.transitionDuration] = transitionTime + 's'
cachedElStyle[plugin.transform] = 'translate3d(' + plugin[slideType].pixelOffset + 'px, ' + offsetTop + 'px, 0)';
} else {
var transitionTime = plugin.getTransitionTime(downY, upY, downYtime, upYtime, "Y");
cachedElStyle[plugin.transitionDuration] = transitionTime + 's'
cachedElStyle[plugin.transform] = 'translate3d(' + offsetLeft + 'px, ' + plugin[slideType].pixelOffset + 'px, 0)';
if (postInView > -1 && downY < upY) {
console.log("scrollade upp");
postInView--;
if (postInView == -1) {
$("body, #topMenu").animate({
backgroundColor: currentColor
},700);
}
else {
$("body, #topMenu").animate({
backgroundColor: colorsArray[postInView]
},700);
}
}
else if (postInView < colorsArray.length-1 && downY > upY) {
postInView++;
console.log("scrollade ner");
$("body, #topMenu").animate({
backgroundColor: colorsArray[postInView]
},700);
}
console.log("postInView:" + postInView);
console.log(plugin);
console.log("curentSlide: " +plugin.vertical.currentSlide);
console.log("offset().top: " +$('#unsavedPostWrapper').offset().top);
}
} else {
sliding = 0;
}
};
plugin.init();
}
$.fn.slideToucher = function (options) {
/* At the moment I am rebuilding plugin from scratch if called twice, need to create destroy and refresh methods for more flexibility */
return this.each(function () {
new slideToucher(this, options)
});
};
})(jQuery, window, document);