Last active
December 14, 2015 01:08
-
-
Save opi/5003488 to your computer and use it in GitHub Desktop.
Touch Event, vanilla !
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
/** | |
* Touch events | |
*/ | |
// wrapper is a DOM element | |
wrapper.touch = {} | |
wrapper.get(0).ontouchstart = function(e) { | |
// Store start position | |
wrapper.touch.x = e.touches[0].clientX; | |
}; | |
wrapper.get(0).ontouchmove = function(e) { | |
// only deal with one finger | |
if (e.touches.length == 1 && !wrapper.is(":animated")) { | |
// Get delta | |
var deltaX = wrapper.touch.x - e.touches[0].clientX; | |
// Move foward | |
if (deltaX > 30) { | |
e.preventDefault(); | |
// DO WHAT YOU NEED WHEN MOVING RIGHT | |
} | |
// Move backward | |
else if (deltaX < -30) { | |
e.preventDefault(); | |
// DO WHAT YOU NEED WHEN MOVING LEFT | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment