-
Notifications
You must be signed in to change notification settings - Fork 17
/
touchslider.js
274 lines (241 loc) · 9.71 KB
/
touchslider.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
/*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
touchslider = {
output: function(/*string*/ msg) {
if (console) {
console.info(msg);
}
},
/**
* We start by creating the sliding grid out of the specified
* element. We'll look for each child with a class of cell when
* we create the slide panel.
*/
createSlidePanel: function(/*string*/ gridid, /*int*/ cellWidth, /*int*/ padding) {
var x = padding;
$(gridid).each(function() {
$(this).css({
'position': 'relative',
'left': '0px'
});
$(this).parent().css('overflow', 'hidden');
$(this).children('.cell').each(function() {
$(this).css({
width: cellWidth + 'px',
height: '90%',
position: 'absolute',
left: x + 'px',
top: padding + 'px'
});
x += cellWidth + padding;
});
/*
Many of the mobile browsers resize the screen and therefore
don't give accurate information about the size of the window.
We need to save this information so we can use it later when
we're sliding the grid.
*/
touchslider.width = x;
touchslider.colWidth = cellWidth + padding;
try {
document.createEvent('TouchEvent');
/*
Now that we've finished the layout we'll make our panel respond
to all of the touch events.
*/
touchslider.makeTouchable(gridid);
} catch (e) {
/*
* Then we aren't on a device that supports touch
*/
$(this).css({
'height': '385px',
'overflow': 'auto'
});
}
});
},
/**
* This function just binds the touch functions for the grid.
* It is very important to stop the default, stop the
* propagation, and return false. If we don't then the touch
* events might cause the regular browser behavior for touches
* and the screen will start sliding around.
*/
makeTouchable: function(/*string*/ gridid) {
$(gridid).each(function() {
this.ontouchstart = function(e) {
touchslider.touchStart($(this), e);
//e.preventDefault();
//e.stopPropagation();
return true;
};
this.ontouchend = function(e) {
e.preventDefault();
e.stopPropagation();
if (touchslider.sliding) {
touchslider.sliding = false;
touchslider.touchEnd($(this), e);
return false;
} else {
/*
We never slid so we can just return true
and perform the default touch end
*/
return true;
}
};
this.ontouchmove = function(e) {
touchslider.touchMove($(this), e);
e.preventDefault();
e.stopPropagation();
return false;
};
});
},
/**
* A little helper to parse off the 'px' at the end of the left
* CSS attribute and parse it as a number.
*/
getLeft: function(/*JQuery*/ elem) {
return parseInt(elem.css('left').substring(0, elem.css('left').length - 2), 10);
},
/**
* When the touch starts we add our sliding class a record a few
* variables about where the touch started. We also record the
* start time so we can do momentum.
*/
touchStart: function(/*JQuery*/ elem, /*event*/ e) {
elem.css({
'-webkit-transition-duration': '0'
});
touchslider.startX = e.targetTouches[0].clientX;
touchslider.startLeft = touchslider.getLeft(elem);
touchslider.touchStartTime = new Date().getTime();
},
/**
* When the touch ends we need to adjust the grid for momentum
* and to snap to the grid. We also need to make sure they
* didn't drag farther than the end of the list in either
* direction.
*/
touchEnd: function(/*JQuery*/ elem, /*event*/ e) {
if (touchslider.getLeft(elem) > 0) {
/*
* This means they dragged to the right past the first item
*/
touchslider.doSlide(elem, 0, '2s');
elem.parent().removeClass('sliding');
touchslider.startX = null;
} else if ((Math.abs(touchslider.getLeft(elem)) + elem.parent().width()) > touchslider.width) {
/*
* This means they dragged to the left past the last item
*/
touchslider.doSlide(elem, '-' + (touchslider.width - elem.parent().width()), '2s');
elem.parent().removeClass('sliding');
touchslider.startX = null;
} else {
/*
This means they were just dragging within the bounds of the grid
and we just need to handle the momentum and snap to the grid.
*/
touchslider.slideMomentum(elem, e);
}
},
/**
* If the user drags their finger really fast we want to push
* the slider a little farther since they were pushing a large
* amount.
*/
slideMomentum: function(/*jQuery*/ elem, /*event*/ e) {
var slideAdjust = (new Date().getTime() - touchslider.touchStartTime) * 10;
var left = touchslider.getLeft(elem);
/*
We calculate the momentum by taking the amount of time they were sliding
and comparing it to the distance they slide. If they slide a small distance
quickly or a large distance slowly then they have almost no momentum.
If they slide a long distance fast then they have a lot of momentum.
*/
var changeX = 12000 * (Math.abs(touchslider.startLeft) - Math.abs(left));
slideAdjust = Math.round(changeX / slideAdjust);
var newLeft = slideAdjust + left;
/*
* We need to calculate the closest column so we can figure out
* where to snap the grid to.
*/
var t = newLeft % touchslider.colWidth;
if ((Math.abs(t)) > ((touchslider.colWidth / 2))) {
/*
* Show the next cell
*/
newLeft -= (touchslider.colWidth - Math.abs(t));
} else {
/*
* Stay on the current cell
*/
newLeft -= t;
}
if (touchslider.slidingLeft) {
var maxLeft = parseInt('-' + (touchslider.width - elem.parent().width()), 10);
/*
* Sliding to the left
*/
touchslider.doSlide(elem, Math.max(maxLeft, newLeft), '0.5s');
} else {
/*
* Sliding to the right
*/
touchslider.doSlide(elem, Math.min(0, newLeft), '0.5s');
}
elem.parent().removeClass('sliding');
touchslider.startX = null;
},
doSlide: function(/*jQuery*/ elem, /*int*/ x, /*string*/ duration) {
elem.css({
left: x + 'px',
'-webkit-transition-property': 'left',
'-webkit-transition-duration': duration
});
},
/**
* While they are actively dragging we just need to adjust the
* position of the grid using the place they started and the
* amount they've moved.
*/
touchMove: function(/*JQuery*/ elem, /*event*/ e) {
if (!touchslider.sliding) {
elem.parent().addClass('sliding');
}
touchslider.sliding = true;
if (touchslider.startX > e.targetTouches[0].clientX) {
/*
* Sliding to the left
*/
elem.css('left', '-' + (touchslider.startX - e.targetTouches[0].clientX - touchslider.startLeft) + 'px');
touchslider.slidingLeft = true;
} else {
/*
* Sliding to the right
*/
var left = (e.targetTouches[0].clientX - touchslider.startX + touchslider.startLeft);
elem.css('left', left + 'px');
touchslider.slidingLeft = false;
}
}
};
$(document).ready(function() {
touchslider.createSlidePanel('#slidebar', 200, 15);
});