Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
vertical and multitouch swipe
  • Loading branch information
tqc committed Sep 20, 2011
commit 90c485a02b8ce461c5d6daf3d5f46905f1320d9b
36 changes: 29 additions & 7 deletions js/jquery.mobile.event.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// add new event shortcuts
$.each( ( "touchstart touchmove touchend orientationchange throttledresize " +
"tap taphold swipe swipeleft swiperight scrollstart scrollstop" ).split( " " ), function( i, name ) {
"tap taphold swipe swipeleft swiperight swipeup swipedown scrollstart scrollstop" ).split( " " ), function( i, name ) {

$.fn[ name ] = function( fn ) {
return fn ? this.bind( name, fn ) : this.trigger( name );
Expand Down Expand Up @@ -125,15 +125,19 @@ $.event.special.swipe = {

setup: function() {
var thisObject = this,
$this = $( thisObject );
$this = $( thisObject ),
touching = false;

$this.bind( touchStartEvent, function( event ) {
if (touching == true) return;
touching = true;
var data = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] : event,
start = {
time: ( new Date() ).getTime(),
coords: [ data.pageX, data.pageY ],
origin: $( event.target )
origin: $( event.target ),
touchcount: event.originalEvent.touches ? event.originalEvent.touches.length : 1
},
stop;

Expand All @@ -159,15 +163,31 @@ $.event.special.swipe = {

$this.bind( touchMoveEvent, moveHandler )
.one( touchStopEvent, function( event ) {
touching=false;
$this.unbind( touchMoveEvent, moveHandler );

if ( start && stop ) {
var eventdata = {start:start, stop:stop};

if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {

start.origin.trigger( "swipe" )
.trigger( start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight" );

eventdata.direction = start.coords[0] > stop.coords[ 0 ] ? "left" : "right";

start.origin.trigger( "swipe", eventdata )
.trigger( "swipe"+eventdata.direction , eventdata);
}

if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {

eventdata.direction = start.coords[1] > stop.coords[ 1 ] ? "up" : "down";

start.origin.trigger( "swipe", eventdata )
.trigger( "swipe"+eventdata.direction , eventdata);

}
}
start = stop = undefined;
Expand Down Expand Up @@ -290,7 +310,9 @@ $.each({
scrollstop: "scrollstart",
taphold: "tap",
swipeleft: "swipe",
swiperight: "swipe"
swiperight: "swipe",
swipeup: "swipe",
swipedown: "swipe"
}, function( event, sourceEvent ) {

$.event.special[ event ] = {
Expand Down