Calculate distance of swipe event?

Calculate distance of swipe event?

I am trying to determine how far a use has swiped. For example. If the user swipes a little I want to close a "drawer" enough to see part of what's behind it. If the user swipes more than half the screen I want to close it completely. I don't see any obvious way to tell how far the swipe has traveled. However, I was able to modify the code enough to add this additional data.


In the definition of $.event.special.swipe there is a handler for the touchStopEvent. In that method I changed this:



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




to this


var evt = jQuery.Event(start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight");
evt.start = start.coords;
evt.stop = stop.coords;
start.origin.trigger( "swipe" ).trigger(evt);

After this change is made the start and stop coords can be accessed in the event handler as event.start and event.stop.

Is there a better way to accomplish this? I prefer not to have to modify jquery mobile myself if I don't have to.

Rob