Skip to content

Instantly share code, notes, and snippets.

@Wilto
Created June 9, 2011 22:37
Show Gist options
  • Save Wilto/1017944 to your computer and use it in GitHub Desktop.
Save Wilto/1017944 to your computer and use it in GitHub Desktop.
Modified jQM Swipe Event
// also handles swipeleft, swiperight
$.event.special.swipe = {
setup: function() {
var thisObject = this,
$this = $( thisObject );
$this
.bind( touchStartEvent, function( event ) {
var data = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] :
event,
start = {
time: (new Date).getTime(),
coords: [ data.pageX, data.pageY ],
origin: $( event.target )
},
stop;
function moveHandler( event ) {
if ( !start ) {
return;
}
var data = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] :
event;
stop = {
time: (new Date).getTime(),
coords: [ data.pageX, data.pageY ]
};
// prevent scrolling
if ( Math.abs( start.coords[0] - stop.coords[0] ) > 10 ) {
event.preventDefault();
}
}
$this
.bind( touchMoveEvent, moveHandler )
.one( touchStopEvent, function( event ) {
$this.unbind( touchMoveEvent, moveHandler );
if ( start && stop ) {
if (stop.time - start.time < 1000 &&
Math.abs(start.coords[0] - stop.coords[0]) > 30 &&
Math.abs(start.coords[1] - stop.coords[1]) < 75) {
var left = start.coords[0] > stop.coords[0];
start.origin
.trigger("swipe", {direction: left ? "left" : "right"})
.trigger(left ? "swipeleft" : "swiperight" );
}
/*/
// Original swipe event, which doesn’t seem to include swipe direction
if ( stop.time - start.time < 1000 &&
Math.abs( start.coords[0] - stop.coords[0]) > 30 &&
Math.abs( start.coords[1] - stop.coords[1]) < 75 ) {
start.origin
.trigger( "swipe" )
.trigger( start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight" );
}
//*/
}
start = stop = undefined;
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment