Created
February 9, 2016 15:02
-
-
Save juanparati/a3452e874ecb6fe38e01 to your computer and use it in GitHub Desktop.
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
var swipeFunc = function() { | |
this._swipe_direction = ['left','right']; | |
this._y_diff = -1; | |
this._x_diff = -1; | |
this._x_min_length = 30; | |
this.touches = { | |
'touchstart': {'entered':false,'x':-1, 'y':-1}, | |
'touchmove' : {'entered':false,'x':-1, 'y':-1}, | |
'touchend' : false, | |
'direction' : 'undetermined' | |
}; | |
}; | |
swipeFunc.prototype = { | |
touchHandler: function(event) { | |
var event_handle = this.handle, touch; | |
if(typeof event !== 'undefined'){ | |
event.preventDefault(); | |
if(event.touches !== 'undefined'){ | |
touch = event.touches[0]; | |
switch (event.type) { | |
case 'touchstart': | |
case 'touchmove': | |
event_handle.touches[event.type].entered = true; | |
event_handle.touches[event.type].x = touch.pageX; | |
event_handle.touches[event.type].y = touch.pageY; | |
break; | |
case 'touchend': | |
event_handle.touches[event.type] = true; | |
if(!event_handle.touches.touchmove.entered) | |
break; | |
event_handle._y_diff = event_handle.touches.touchstart.y > event_handle.touches.touchmove.y ? event_handle.touches.touchstart.y - event_handle.touches.touchmove.y : event_handle.touches.touchmove.y - event_handle.touches.touchstart.y; | |
event_handle._x_diff = event_handle.touches.touchstart.x > event_handle.touches.touchmove.x ? event_handle.touches.touchstart.x - event_handle.touches.touchmove.x : event_handle.touches.touchmove.x - event_handle.touches.touchstart.x; | |
if (event_handle.touches.touchstart.x > -1 && event_handle.touches.touchmove.x > -1 && event_handle._x_diff > event_handle._x_length_sensitivity && event_handle._x_diff >= event_handle._y_diff) { | |
event_handle.touches.direction = event_handle.touches.touchstart.x < event_handle.touches.touchmove.x ? 'right' : 'left'; | |
// DO STUFF HERE | |
//alert(event_handle.touches.direction); | |
// checking the swipe direction example: | |
for(var k in event_handle._swipe_direction){ | |
if(event_handle.touches.direction == event_handle._swipe_direction[k]) | |
alert(event_handle.touches.direction); | |
} | |
} | |
default: | |
break; | |
} | |
} | |
} | |
}, | |
init: function(opts) { | |
this._swipe_direction = opts.swipe_dir !== undefined ? opts.swipe_dir : this._swipe_direction; | |
this._x_min_length = opts.x_min_length !== undefined ? opts.x_min_length : this._x_min_length; | |
document.getElementsByClassName(opts.main_target)[0].addEventListener('touchstart', this.touchHandler, false); | |
document.getElementsByClassName(opts.main_target)[0].addEventListener('touchmove', this.touchHandler, false); | |
document.getElementsByClassName(opts.main_target)[0].addEventListener('touchend', this.touchHandler, false); | |
document.getElementsByClassName(opts.main_target)[0].handle = this; | |
} | |
}; | |
var swipeFunc_1 = new swipeFunc(), swipeFunc_2 = new swipeFunc(); | |
// default left/right swipe detection | |
swipeFunc_1.init({ | |
main_target:'class_name_of_target1' | |
}); | |
// catch only left swiping, with adjustable minimum swipe length | |
swipeFunc_2.init({ | |
main_target:'class_name_of_target2', | |
swipe_dir: ['left'], | |
x_min_length: 40 | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment