How to Swipe Top Down Jquery Mobile

How to swipe top down JQuery mobile

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

Jquery Mobile Panel Swipe Event and Tabs Swipe Event Inside a page?

To add the external panel, you can use the regular jQuery document ready:

var panel = '<div data-theme="a" data-role="panel" data-display="overlay" id="leftpanel"><ul data-role="listview"><li data-icon="false"><a data-ajax="false" href="index.html">Home</a></li><li data-icon="false"><a data-ajax="false" href="html/examples.html">Examples</a></li><li data-icon="false"><a data-ajax="false" href="html/custom/version.html">Version 1.0.1</a></li></ul></div>';

$(function () {
$("body").prepend(panel);
$("[data-role=panel]").panel().enhanceWithin();
});

Then you could listen for swipes on the content div to change tabs and on the header div to open/close the panel:

$("div[data-role=content]").on("swipeleft", function (event) {
changeNavTab(true);
});
$("div[data-role=content]").on("swiperight", function (event) {
changeNavTab(false);
});

// Navigation Drawer Swipe Listener
$("div[data-role=header]").on("swipeleft swiperight", function (e) {
// save swipe direction right/left
var dir = 'prev';
if (e.type == 'swiperight') {
dir = 'next';
}
if (dir == 'prev') {
$('#leftpanel').panel('close');

} else {
$('#leftpanel').panel('open');

}
});

Updated FIDDLE

what is the event for vertical swipe

Unfortunately swipeup and swipedown don't exist in jQuery mobile and they are not planed for version 1.3. There is an 3rd party plugin here: http://developingwithstyle.blogspot.com/2010/11/jquery-mobile-swipe-up-down-left-right.html. Use them like you would use normal events: swipedown and swipeup.

You can also use this plugin if you need this implementation: http://www.netcu.de/jquery-touchwipe-iphone-ipad-library. It also works on Android devices. This is a worst case scenario solution because unlike official event there ones don't work on desktop browsers.

Here's an working example, test it on mobile devices only: http://jsfiddle.net/Gajotres/WYnnk/

    $("#wrapper").touchwipe({
wipeLeft: function() {
$("#carousel").trigger("next", 1);
},
wipeRight: function() {
$("#carousel").trigger("next", 1);
},
min_move_x: 20,
min_move_y: 20,
preventDefaultEvents: true
});

Detect a finger swipe through JavaScript on the iPhone and Android

Simple vanilla JS code sample:

document.addEventListener('touchstart', handleTouchStart, false);        
document.addEventListener('touchmove', handleTouchMove, false);

var xDown = null;
var yDown = null;

function getTouches(evt) {
return evt.touches || // browser API
evt.originalEvent.touches; // jQuery
}

function handleTouchStart(evt) {
const firstTouch = getTouches(evt)[0];
xDown = firstTouch.clientX;
yDown = firstTouch.clientY;
};

function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}

var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;

var xDiff = xDown - xUp;
var yDiff = yDown - yUp;

if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
/* right swipe */
} else {
/* left swipe */
}
} else {
if ( yDiff > 0 ) {
/* down swipe */
} else {
/* up swipe */
}
}
/* reset values */
xDown = null;
yDown = null;
};

Tested in Android.



Related Topics



Leave a reply



Submit