-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.yatouchslider.js
executable file
·185 lines (151 loc) · 6.82 KB
/
jquery.yatouchslider.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
/**
* jQuery yaTouchSlider plugin
*
* Copyright (c) 2010-2011 Kir Belevich ([email protected])
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* @version 0.2
*/
(function($) {
$.fn.yaTouchSlider = function(options) {
var defaults = {
step: undefined, // step
threshold: 30, // threshold (pixels) that must be overcome
acceleration: true, // enable/disable slider acceleration boost
preventVert: true, // prevent vertical scroll while sliding
preventVertThreshold: 5 // vertical scroll preventing threshold (pixels)
},
step;
options = $.extend(defaults, options);
step = options.step || window.innerWidth; // step or the whole screen width
return this.each(function(i, el) {
var x1, shiftX,
y1, shiftY,
t1,
width = $(el).outerWidth(), // actual width
currentX = (new WebKitCSSMatrix(getComputedStyle(el).webkitTransform)).m41, // initial shift
currentI = ~~(-currentX / step), // initial index
limitX = window.innerWidth - width, // max shift
limitI = Math.ceil(-limitX / step) || 1; // max index
if (width > window.innerWidth) {
function slide(shift) {
shiftAbs = Math.abs(shift);
var timeShift = Date.now() - t1,
speed = shiftAbs / timeShift, // pixels in ms
accel = 1,
animationTime = '0.2';
// slider acceleration
if (options.acceleration) {
accel = speed > 0.3 && speed < 0.6 ? 2 :
speed >= 0.6 && speed < 1 ? 3 :
speed >= 1 ? 4 :
1;
animationTime = accel >= 3 ? '0.3' : '0.2';
}
if (shiftAbs > options.threshold || !t1) {
// more than one step
if (shiftAbs > step) {
currentX += ~~(shift/step)*step;
}
// left or right direction
if (shift > 0) {
currentX += step * accel;
currentI -= accel;
} else if (shift < 0) {
currentX -= step * accel;
currentI += accel;
}
// left or right limit
if (currentX > 0) {
currentX = currentI = 0;
} else if (currentX < limitX) {
currentX = limitX;
currentI = limitI;
}
}
// callback after each slide
if (options.callback) {
$(el).one('webkitTransitionEnd', function() {
options.callback({
currentX: currentX,
limitX: limitX,
currentI: currentI,
limitI: limitI,
speed: speed.toFixed(2),
timeShift: timeShift,
acceleration: accel,
animationTime: animationTime
});
});
}
// animate to calculated position
$(el).css({
'-webkit-transition':'-webkit-transform ' + animationTime + 's ease-out',
'-webkit-transform': 'translate3d(' + currentX + 'px, 0, 0)'
});
// reset
x1 = y1 = shiftX = shiftY = t1 = undefined;
};
$(el)
.css('-webkit-transform', 'translateZ(0)')
.bind({
// start
'touchstart.touchSlides': function(e) {
var eo = e.originalEvent.touches[0];
x1 = eo.pageX;
y1 = eo.pageY;
$(this).css('-webkit-transition', 'none');
t1 = Date.now();
},
// move
'touchmove.touchSlides': function(e) {
var eo = e.originalEvent.touches[0];
shiftX = eo.pageX - x1;
shiftY = eo.pageY - y1;
$(el).css('-webkit-transform', 'translate3d(' + (currentX + shiftX ) + 'px, 0, 0)');
if (options.preventVert && Math.abs(shiftY) > options.preventVertThreshold) {
e.preventDefault();
}
},
// end
'touchend.touchSlides': function(e) {
slide(shiftX);
},
// cancel / reset
'touchcancel.touchSlides': function() {
x1 = y1 = shiftX = shiftY = t1 = undefined;
},
// left custom slide event
'slideLeft.touchSlides': function(e, customStep) {
slide(customStep || step);
},
// right custom slide event
'slideRight.touchSlides': function(e, customStep) {
slide(-customStep || -step);
}
});
// correction of current position after device rotation
$(window).bind('orientationchange', function() {
step = options.step || window.innerWidth;
if (Math.abs(window.orientation) == 90 && currentX - limitX <= window.innerWidth) {
currentX = limitX = window.innerWidth - width;
currentI++;
slide(0);
} else {
limitX = window.innerWidth - width;
currentI = currentI - Math.ceil((currentX - limitX)/step);
slide(0);
}
});
}
});
};
// untouch
$.fn.yaUntouchSlider = function() {
return this.each(function(i, el) {
$(el).unbind('.touchSlides');
});
};
})(jQuery);