Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swipe top down JQuery mobile

I'm trying to make event on swiping up and down instead of left and right

i have this roll as image shows: how to make swipe top down

I can handle the event using arrow icon (onClick()) but i want to add swipe up down event, when adding swipe event it works on left right I want it up down as image displays any help?

like image 838
Muath Avatar asked Jun 16 '13 09:06

Muath


2 Answers

jQuery Mobile natively provides us with the ability to capture the swipeleft and swiperight. It does not however offer us swipeup and swipedown out of the box. Adapting what the jQuery team has done for swipeleft and swiperight, we are able to create and capture those events in the same manner. See the following code to implement swipeup and swipedown:

(function() {     var supportTouch = $.support.touch,             scrollEvent = "touchmove scroll",             touchStartEvent = supportTouch ? "touchstart" : "mousedown",             touchStopEvent = supportTouch ? "touchend" : "mouseup",             touchMoveEvent = supportTouch ? "touchmove" : "mousemove";     $.event.special.swipeupdown = {         setup: function() {             var thisObject = this;             var $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[1] - stop.coords[1]) > 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[1] - stop.coords[1]) > 30 &&                                 Math.abs(start.coords[0] - stop.coords[0]) < 75) {                             start.origin                                     .trigger("swipeupdown")                                     .trigger(start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown");                         }                     }                     start = stop = undefined;                 });             });         }     };     $.each({         swipedown: "swipeupdown",         swipeup: "swipeupdown"     }, function(event, sourceEvent){         $.event.special[event] = {             setup: function(){                 $(this).bind(sourceEvent, $.noop);             }         };     });  })(); 

and here is the answer from Blackdynamo

like image 109
Taymoor Q. Avatar answered Sep 22 '22 14:09

Taymoor Q.


I had Problems with the accepted answer here, because swiping wasn't detected when origin and target of the swipe were not the same.

Here maybe an easier answer, where i directly override the jquery handleSwipe event (based on jquery.mobile-1.4.5) and append it with a vertical swipe, called up and down:

(function( $, window, undefined ) {      //custom handleSwipe with swiperight, swipeleft, swipeup, swipedown     $.event.special.swipe.handleSwipe = function( start, stop, thisObject, origTarget ) {         if ( stop.time - start.time < $.event.special.swipe.durationThreshold ) {             var horSwipe = Math.abs( start.coords[0] - stop.coords[0] ) > $.event.special.swipe.horizontalDistanceThreshold;             var verSwipe = Math.abs( start.coords[1] - stop.coords[1] ) > $.event.special.swipe.verticalDistanceThreshold;             if( horSwipe != verSwipe ) {                 var direction;                 if(horSwipe)                     direction = start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight";                 else                     direction = start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown";                 $.event.trigger($.Event( "swipe", { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);                 $.event.trigger($.Event( direction, { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);                 return true;             }             return false;         }         return false;     }      //do binding     $.each({         swipeup: "swipe.up",         swipedown: "swipe.down"     }, function( event, sourceEvent ) {         $.event.special[ event ] = {             setup: function() {                 $( this ).bind( sourceEvent, $.noop );             },             teardown: function() {                 $( this ).unbind( sourceEvent );             }         };     });  })( jQuery, this ); 
like image 29
kaiser Avatar answered Sep 20 '22 14:09

kaiser