Skip to content

Instantly share code, notes, and snippets.

@dbushell
Created May 23, 2012 08:06
Show Gist options
  • Save dbushell/2773801 to your computer and use it in GitHub Desktop.
Save dbushell/2773801 to your computer and use it in GitHub Desktop.
Touchscreen swipe events for image rotator
if (Modernizr.touch) {
var hero = $('#hero');
var touchCancel = function()
{
hero[0].removeEventListener('touchmove', touchMove, false);
hero[0].data('touch', false);
};
var touchMove = function(e)
{
if (hero.data('_touch')) {
var dx = hero.data('_touchX') - e.touches[0].pageX;
var dy = hero.data('_touchY') - e.touches[0].pageY;
if (Math.abs(dx) >= 20) {
touchCancel();
if (dx < 0) {
hero.find('.nav-prev').trigger('click');
} else {
hero.find('.nav-next').trigger('click');
}
}
}
};
hero[0].addEventListener('touchstart', function(e)
{
if (e.touches.length === 1) {
hero.data('_touchX', e.touches[0].pageX);
hero.data('_touchY', e.touches[0].pageY);
hero.data('_touch', true);
hero[0].addEventListener('touchmove', touchMove, false);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment