Skip to content

Instantly share code, notes, and snippets.

@lyzadanger
Created December 9, 2011 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyzadanger/1449628 to your computer and use it in GitHub Desktop.
Save lyzadanger/1449628 to your computer and use it in GitHub Desktop.
Quickie swipe thingy with jQuery
var Swiper = function(el) {
var startX, startY, lastX, lastY;
var minswipe;
var init=function(el) {
Swiper.minswipe = 50;
el.bind('touchstart', function(event) {
Swiper.startX = event.originalEvent.targetTouches[0].screenX;
Swiper.startY = event.originalEvent.targetTouches[0].screenY;
$(this).bind('touchmove', function(event) {
Swiper.lastX = event.originalEvent.targetTouches[0].screenX;
Swiper.lastY = event.originalEvent.targetTouches[0].screenY;
if(Math.abs(Swiper.startX - Swiper.lastX) > Swiper.minswipe) {
$(this).unbind('touchmove');
$(this).trigger('swiper');
if (Swiper.lastX > Swiper.startX) {
$(this).trigger('swiperedright');
}
else {
$(this).trigger('swiperedleft');
}
Swiper.reset();
}
});
});
}(el);
var reset = function() {
Swiper.startX = 0;
Swiper.startY = 0;
Swiper.lastX = 0;
Swiper.lastY = 0;
};
};
$(document).ready(function() {
if (Modernizr.touch) {
Swiper($('#tracktouches'));
Swiper($('#tracktouches2'));
$('#tracktouches').bind('swiperedleft', function() {
alert('tracktouches ONE went left');
});
$('#tracktouches2').bind('swiperedleft', function() {
alert('tracktouches TWO went left');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment