Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function swipedetect(el, callback){
- var touchsurface = el,
- swipedir,
- startX,
- startY,
- distX,
- distY,
- threshold = 100, //required min distance traveled to be considered swipe
- restraint = 170, // maximum distance allowed at the same time in perpendicular direction
- allowedTime = 400, // maximum time allowed to travel that distance
- touchTime = 300,
- touchDist = 30,
- elapsedTime,
- startTime,
- handleswipe = callback || function(swipedir){}
- touchsurface.addEventListener('touchstart', function(e){
- var touchobj = e.changedTouches[0]
- swipedir = 'none'
- dist = 0
- startX = touchobj.pageX
- startY = touchobj.pageY
- startTime = new Date().getTime() // record time when finger first makes contact with surface
- }, false)
- touchsurface.addEventListener('touchend', function(e){
- var touchobj = e.changedTouches[0]
- distX = touchobj.pageX - startX // get horizontal dist traveled by finger while in contact with surface
- distY = touchobj.pageY - startY // get vertical dist traveled by finger while in contact with surface
- elapsedTime = new Date().getTime() - startTime // get time elapsed
- if (elapsedTime <= allowedTime){ // first condition for awipe met
- if (Math.abs(distX) >= threshold && Math.abs(distY) <= restraint){
- swipedir = (distX < 0)? 'left' : 'right'
- e.preventDefault();
- handleswipe(swipedir, touchobj.pageX, touchobj.pageY);
- } else if(elapsedTime <= touchTime) {
- if(Math.abs(distX) <= touchDist && Math.abs(distY) <= touchDist) swipedir = 'touch';
- handleswipe(swipedir, touchobj.pageX, touchobj.pageY);
- }
- }
- }, false)
- }
Advertisement
Add Comment
Please, Sign In to add comment