-
Notifications
You must be signed in to change notification settings - Fork 1
/
addswipe.js
222 lines (168 loc) · 4.54 KB
/
addswipe.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
// addSwipe :: adding swipe gesture support to a layer
// http://lazaworx.com/addswipe-plugin/
(function($) {
$.extend( $.support, {
touch: "ontouchend" in document
});
// Easing function by George Smith
$.extend( jQuery.easing, {
easeOutBack: function (x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
},
easeOutQuad: function (x, t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
}
});
$.fn.addSwipe = function( leftFn, rightFn, settings ) {
settings = $.extend( {}, $.fn.addSwipe.defaults, settings );
var effect = settings.snapGrid? 'easeOutBack' : 'easeOutQuad';
return this.each(function() {
var t = $(this);
var xs, dist, ex = 0, tx = 0, tx1 = 0, x0, tt, speed;
t.attr('draggable', 'true');
// retrieving the event's X position
var getX = function(e) {
return ex = ( e.touches && e.touches.length > 0 )? e.touches[0].clientX : ( e.clientX ? e.clientX : ex );
};
// registering start position
var setPos = function(e) {
tx = getX(e);
};
var noAction = function(e) {
return false;
};
// moving the element
var dragMove = function(e) {
if ( tx ) {
t.css({
left: (getX(e) - tx + x0)
});
} else { // Wrong dragstart event coordinate :: starting now
tx = getX(e);
}
return false;
};
// stopped dragging
var dragStop = function(e) {
var dx = getX(e) - tx;
tx1 = t.position().left;
// detach handlers
if ( $.support.touch ) {
this.ontouchmove = null;
this.ontouchend = null;
} else {
$(document).off('mousemove', dragMove).off('mouseup click', dragStop);
}
if ( Math.abs(dx) > settings.minDist ) {
// Swipe detected
var x1, cw = t.parent().width(), tw = t.width(), gw, eff = effect;
speed = 1000 * dx / (new Date().getTime() - tt);
x1 = tx1 + Math.round(speed / 2);
if ( settings.snapGrid ) {
gw = cw / settings.snapGrid;
x1 = Math.round(Math.round(x1 / gw) * gw);
}
if ( settings.keepWithin ) {
if ( x1 < 0 && (tw + x1) < cw ) {
x1 = cw - tw;
eff = 'easeOutBack';
} else if ( x1 > 0 && (tw + x1) > cw ) {
x1 = 0;
eff = 'easeOutBack';
}
}
t.animate({
left: x1
}, 500, eff);
// Calling the appropriate function
if ( dx < 0 ) {
if ( $.isFunction(leftFn) ) {
leftFn.call();
}
} else if ( $.isFunction(rightFn) ) {
rightFn.call();
}
} else {
// Just a small move - let the click event happen
t.animate({
left: x0
}, 200);
t.trigger('click');
}
return false;
};
// registering start position on touch devices
var touchStart = function(e) {
if ( (e.type === 'touchstart' || e.type === 'touchmove') &&
(!e.touches || e.touches.length > 1 || t.is(':animated')) ) {
// >= 2 finger flick
return true;
}
setPos(e);
dragStart(e);
};
// start dragging
var dragStart = function(e) {
t.stop(true, false);
x0 = t.position().left;
tt = new Date().getTime();
dist = 0;
if ( $.support.touch ) {
this.ontouchmove = dragMove;
this.ontouchend = dragStop;
return true;
} else {
t.off('click');
t.click(noAction);
$(document).on({
'mousemove': dragMove,
'mouseup': dragStop
});
e.cancelBubble = true;
return false;
}
};
// initializing
if ($.support.touch) {
this.ontouchstart = touchStart;
} else {
t.on({
'dragstart': dragStart,
'mousedown': setPos
});
}
// reset function
xs = t.position().left;
t.on('resetswipe', function() {
t.stop(true, false).animate({
left: xs
}, 500, 'easeOutBack');
return false;
});
// removing the event handler
t.on('unswipe', function() {
if ( $.support.touch ) {
this.ontouchmove = null;
this.ontouchend = null;
this.ontouchstart = null;
} else {
if ( $.isFunction(t.noAction) ) {
t.off(noAction);
}
if ( $.isFunction(t.dragStart) ) {
t.off(dragStart);
}
$(document).off('mousemove', dragMove).off('mouseup', dragStop);
}
});
// disabling text selection, because it conflicts with drag
t.on('selectstart', noAction);
});
};
$.fn.addSwipe.defaults = {
minDist: 40,
snapGrid: 0,
keepWithin: true
};
})(jQuery);