Created
May 23, 2012 08:06
-
-
Save dbushell/2773801 to your computer and use it in GitHub Desktop.
Touchscreen swipe events for image rotator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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