Skip to content

Instantly share code, notes, and snippets.

@keeganbrown
Created March 6, 2013 15:13
Show Gist options
  • Save keeganbrown/5099954 to your computer and use it in GitHub Desktop.
Save keeganbrown/5099954 to your computer and use it in GitHub Desktop.
Cycle 2 Touch Swipe Detection, without iOS6 workaround
//TOUCH EVENTS
polyfillRequestAnimFrame(window);
var touchDragX = 0;
var lastTouchX = 0;
var sliderapi = null;
function onTouchStart (event) {
touchDragX = 0;
var touchPos = getTouchPos(event);
lastTouchX = touchPos.pageX;
sliderapi = $(this).parent(".slideshow").data( 'cycle.API' );
}
function onTouchMove (event) {
var touchPos = getTouchPos(event);
touchDragX = touchPos.pageX - lastTouchX;
}
function getTouchPos (event) {
if ( !!event && !!event.originalEvent && !!event.originalEvent.touches && !!event.originalEvent.touches[0] ) {
return ({ pageX: event.originalEvent.touches[0].pageX, pageY: event.originalEvent.touches[0].pageY });
} else if ( !!event && ( !!event.pageX || !!event.pageY ) ) {
return ({ pageX: event.pageX, pageY: event.pageY });
}
return ({ pageX: 0, pageY: 0 });
}
function checkSwipe() {
if ( touchDragX > 100 || touchDragX < -100 ) {
var method = ""
if ( touchDragX > 0 ) {
method = "prev";
} else if ( touchDragX < 0 ) {
method = "next";
}
touchDragX = 0;
if ( !!sliderapi && !!sliderapi[method] ) {
sliderapi[method]();
sliderapi = null;
}
}
window.requestAnimationFrame( checkSwipe );
}
checkSwipe();
//$(".slideshow").on("mousedown", ".slide", onTouchStart );
$(".cycle-slideshow").on("touchstart", ".slide", onTouchStart );
//$(".slideshow").on("mouseup", ".slide", onTouchEnd );
$(".cycle-slideshow").on("touchmove", ".slide", onTouchMove );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment