From 0c53a8972642ae90b865aae45d61f19aba807366 Mon Sep 17 00:00:00 2001 From: Walid Date: Sat, 20 Apr 2013 14:20:12 +0300 Subject: [PATCH] added vertical swiping based solely on paraphrasing --- jquery.cycle2.swipe.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/jquery.cycle2.swipe.js b/jquery.cycle2.swipe.js index caa15a9..be6a62d 100644 --- a/jquery.cycle2.swipe.js +++ b/jquery.cycle2.swipe.js @@ -10,8 +10,8 @@ var supportTouch = 'ontouchend' in document; $.event.special.swipe = $.event.special.swipe || { scrollSupressionThreshold: 10, // More than this horizontal displacement, and we will suppress scrolling. durationThreshold: 1000, // More time than this, and it isn't a swipe. - horizontalDistanceThreshold: 30, // Swipe horizontal displacement must be more than this. - verticalDistanceThreshold: 75, // Swipe vertical displacement must be less than this. + distanceThresholdMin: 30, // Swipe horizontal displacement must be more than this. + distanceThresholdMax: 75, // Swipe vertical displacement must be less than this. setup: function() { var $this = $( this ); @@ -36,7 +36,7 @@ $.event.special.swipe = $.event.special.swipe || { }; // prevent scrolling - if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.scrollSupressionThreshold ) { + if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.scrollSupressionThreshold || Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) > $.event.special.swipe.scrollSupressionThreshold) { event.preventDefault(); } } @@ -45,26 +45,40 @@ $.event.special.swipe = $.event.special.swipe || { .one( 'touchend', function( event ) { $this.unbind( 'touchmove', moveHandler ); - if ( start && 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" ); + if ( start && stop ) + { + if ( stop.time - start.time < $.event.special.swipe.durationThreshold) + { + var event_; + if( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.distanceThresholdMin && + Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.distanceThresholdMax ) + { + event_ = start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight"; + } + else if (Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) > $.event.special.swipe.distanceThresholdMin && + Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) < $.event.special.swipe.distanceThresholdMax) + { + event_ = start.coords[1] > stop.coords[ 1 ] ? "swipeup" : "swipedown"; + } } + start.origin.trigger( "swipe" ) + .trigger( event_ ); } - start = stop = undefined; + + start = stop = undefined; }); }); } }; + $.event.special.swipeleft = $.event.special.swipeleft || { setup: function() { $( this ).bind( 'swipe', $.noop ); } }; $.event.special.swiperight = $.event.special.swiperight || $.event.special.swipeleft; +$.event.special.swipeup = $.event.special.swipeup || $.event.special.swipeleft; +$.event.special.swipedown = $.event.special.swipedown || $.event.special.swipeleft; })(jQuery);