Skip to content

Commit 11d4a62

Browse files
committed
verticalswipe and horizontalswipe functions
Added veritcal swipes by creating a new function that swaps out the variables and equations to look for vertical swipes, because vertical layouts are just as cool. Note that this was working in a different custom copy of jquery mobile I was working with. I though of combining all the directions into one function but then thought it would save little in terms of file size or computing resources, and this way is easier for the end-user.
1 parent aa39ca3 commit 11d4a62

File tree

1 file changed

+79
-11
lines changed

1 file changed

+79
-11
lines changed

js/events/touch.js

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
1010
// add new event shortcuts
1111
$.each( ( "touchstart touchmove touchend " +
1212
"tap taphold " +
13-
"swipe swipeleft swiperight " +
13+
"horizontalswipe swipeleft swiperight verticalswipe swipeup swipedown" +
1414
"scrollstart scrollstop" ).split( " " ), function( i, name ) {
1515

1616
$.fn[ name ] = function( fn ) {
@@ -124,8 +124,8 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
124124
};
125125

126126
// also handles swipeleft, swiperight
127-
$.event.special.swipe = {
128-
scrollSupressionThreshold: 30, // More than this horizontal displacement, and we will suppress scrolling.
127+
$.event.special.horizontalswipe = {
128+
scrollSupressionThreshold: 10, // More than this horizontal displacement, and we will suppress scrolling.
129129

130130
durationThreshold: 1000, // More time than this, and it isn't a swipe.
131131

@@ -139,7 +139,7 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
139139

140140
$this.bind( touchStartEvent, function( event ) {
141141
var data = event.originalEvent.touches ?
142-
event.originalEvent.touches[ 0 ] : event,
142+
event.originalEvent.touches[ 0 ] : event,
143143
start = {
144144
time: ( new Date() ).getTime(),
145145
coords: [ data.pageX, data.pageY ],
@@ -154,15 +154,15 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
154154
}
155155

156156
var data = event.originalEvent.touches ?
157-
event.originalEvent.touches[ 0 ] : event;
157+
event.originalEvent.touches[ 0 ] : event;
158158

159159
stop = {
160160
time: ( new Date() ).getTime(),
161161
coords: [ data.pageX, data.pageY ]
162162
};
163163

164164
// prevent scrolling
165-
if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.scrollSupressionThreshold ) {
165+
if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.horizontalswipe.scrollSupressionThreshold ) {
166166
event.preventDefault();
167167
}
168168
}
@@ -172,9 +172,9 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
172172
$this.unbind( touchMoveEvent, moveHandler );
173173

174174
if ( start && stop ) {
175-
if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
176-
Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
177-
Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {
175+
if ( stop.time - start.time < $.event.special.horizontalswipe.durationThreshold &&
176+
Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.horizontalswipe.horizontalDistanceThreshold &&
177+
Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.horizontalswipe.verticalDistanceThreshold ) {
178178

179179
start.origin.trigger( "swipe" )
180180
.trigger( start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight" );
@@ -185,11 +185,79 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
185185
});
186186
}
187187
};
188+
189+
// also handles swipeup, swipedown
190+
$.event.special.verticalswipe = {
191+
scrollSupressionThreshold: 30, // More than this vertical displacement, and we will suppress scrolling.
192+
193+
durationThreshold: 1000, // More time than this, and it isn't a swipe.
194+
195+
horizontalDistanceThreshold: 45, // Swipe horizontal displacement must be less than this.
196+
197+
verticalDistanceThreshold: 85, // Swipe vertical displacement must be more than this.
198+
199+
setup: function() {
200+
var thisObject = this,
201+
$this = $( thisObject );
202+
203+
$this.bind( touchStartEvent, function( event ) {
204+
var data = event.originalEvent.touches ?
205+
event.originalEvent.touches[ 0 ] : event,
206+
start = {
207+
time: ( new Date() ).getTime(),
208+
coords: [ data.pageX, data.pageY ],
209+
origin: $( event.target )
210+
},
211+
stop;
212+
213+
function moveHandler( event ) {
214+
215+
if ( !start ) {
216+
return;
217+
}
218+
219+
var data = event.originalEvent.touches ?
220+
event.originalEvent.touches[ 0 ] : event;
221+
222+
stop = {
223+
time: ( new Date() ).getTime(),
224+
coords: [ data.pageX, data.pageY ],
225+
};
226+
227+
//console.log("startY:"+start.coords[1]+"ZY:"+stop.coords[1])
228+
229+
// prevent scrolling
230+
if ( Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) > $.event.special.verticalswipe.scrollSupressionThreshold ) {
231+
event.preventDefault();
232+
}
233+
}
234+
235+
$this.bind( touchMoveEvent, moveHandler )
236+
.one( touchStopEvent, function( event ) {
237+
$this.unbind( touchMoveEvent, moveHandler );
238+
239+
if ( start && stop ) {
240+
if ( stop.time - start.time < $.event.special.verticalswipe.durationThreshold &&
241+
Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) > $.event.special.verticalswipe.verticalDistanceThreshold &&
242+
Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) < $.event.special.verticalswipe.horizontalDistanceThreshold ) {
243+
244+
start.origin.trigger( "swipe" )
245+
.trigger( start.coords[ 1 ] < stop.coords[ 1 ] ? "swipedown" : "swipeup" );
246+
}
247+
}
248+
start = stop = undefined;
249+
});
250+
});
251+
}
252+
};
253+
188254
$.each({
189255
scrollstop: "scrollstart",
190256
taphold: "tap",
191-
swipeleft: "swipe",
192-
swiperight: "swipe"
257+
swipeleft: "horizontalswipe",
258+
swiperight: "horizontalswipe",
259+
swipeup: "verticalswipe",
260+
swipedown: "verticalswipe"
193261
}, function( event, sourceEvent ) {
194262

195263
$.event.special[ event ] = {

0 commit comments

Comments
 (0)