Skip to content

Instantly share code, notes, and snippets.

@volodymyrlut
Created March 16, 2015 13:28
Show Gist options
  • Save volodymyrlut/21e6f8095a4bfa45e036 to your computer and use it in GitHub Desktop.
Save volodymyrlut/21e6f8095a4bfa45e036 to your computer and use it in GitHub Desktop.
var maxTime = 1000,
// allow movement if < 1000 ms (1 sec)
maxDistance = 50,
// swipe movement of 50 pixels triggers the swipe
target = $(document),
startX = 0,
startTime = 0,
touch = "ontouchend" in document,
startEvent = (touch) ? 'touchstart' : 'mousedown',
moveEvent = (touch) ? 'touchmove' : 'mousemove',
endEvent = (touch) ? 'touchend' : 'mouseup';
target.bind(startEvent, function(e) {
// prevent image drag (Firefox)
e.preventDefault();
startTime = e.timeStamp;
startX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX;
}).bind(endEvent, function(e) {
startTime = 0;
startX = 0;
}).bind(moveEvent, function(e) {
e.preventDefault();
var currentX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX,
currentDistance = (startX === 0) ? 0 : Math.abs(currentX - startX),
// allow if movement < 1 sec
currentTime = e.timeStamp;
if (startTime !== 0 && currentTime - startTime < maxTime && currentDistance > maxDistance) {
if (currentX < startX) {
if($rootScope.showMenuValue == true){
$rootScope.showMenuValue = false;
} else if ($rootScope.current_page!=='dashboard'){
console.log('handled. redirecting u, dude');
$scope.back();
}
}
if (currentX > startX) {
if($rootScope.showMenuValue == false){
$rootScope.showMenuValue = true;
}
}
startTime = 0;
startX = 0;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment