Created
June 9, 2011 22:37
-
-
Save Wilto/1017944 to your computer and use it in GitHub Desktop.
Modified jQM Swipe Event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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