Skip to content

Instantly share code, notes, and snippets.

@mattsahr
Forked from localpcguy/swipeFunc.js
Last active December 21, 2015 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsahr/6340490 to your computer and use it in GitHub Desktop.
Save mattsahr/6340490 to your computer and use it in GitHub Desktop.
Menu Swipe Handler
var swipeFunc = {
touches : {
"touchstart": {"x":-1, "y":-1},
"touchmove" : {"x":-1, "y":-1},
"touchend" : false,
"direction" : "undetermined"
},
touchHandler: function(event) {
var touch;
if (typeof event !== 'undefined'){
event.preventDefault();
if (typeof event.touches !== 'undefined') {
touch = event.touches[0];
switch (event.type) {
case 'touchstart':
case 'touchmove':
swipeFunc.touches[event.type].x = touch.pageX;
swipeFunc.touches[event.type].y = touch.pageY;
break;
case 'touchend':
touches[event.type] = true;
if (swipeFunc.touches.touchstart.x > -1 && swipeFunc.touches.touchmove.x > -1) {
swipeFunc.touches.direction = swipeFunc.touches.touchstart.x < swipeFunc.touches.touchmove.x ? "right" : "left";
// DO STUFF HERE
alert(touches.direction);
}
default:
break;
}
}
}
},
init: function() {
document.addEventListener('touchstart', swipeFunc.touchHandler, false);
document.addEventListener('touchmove', swipeFunc.touchHandler, false);
document.addEventListener('touchend', swipeFunc.touchHandler, false);
}
};
swipeFunc.init();
// ============= SIDE RIGHT MENU - SWIPE HANDLER ===================
var SRM = document.getElementById('SRMmid');
SRM.addEventListener("touchstart", aPP.srmTouchStart, false);
srmTouchStart: function(event) {
aPP.srmSwipeX = event.targetTouches[0].clientX;
aPP.srmSwipeY = event.targetTouches[0].clientY;
var SRMT = document.getElementById('SRMmid');
SRMT.addEventListener("touchend", aPP.srmTouchEnd, false);
// $('#AlertBox').append('<p>R2</p>').css({'display':'block'});
},
srmTouchEnd: function(event) {
var endX = event.changedTouches[0].clientX;
var endY = event.changedTouches[0].clientY;
var dragX = parseInt(endX - aPP.srmSwipeX);
var dragY = parseInt(endY - aPP.srmSwipeY);
if ( ((Math.abs(dragX/dragY)) > 1.2) && (dragX > 40) ) {
aPP.srmClose();
}
// $('#AlertBox').append('<p> dx'+dragX+' dy'+dragY+'</p>').css({'display':'block'});
var SRMT = document.getElementById('SRMmid');
SRMT.removeEventListener("touchend", aPP.srmTouchEnd, false);
},
srmToggle: function(event) {
if ( $('#SRMenu').hasClass('expanded') ) {
aPP.srmClose();
} else {
aPP.srmOpen();
};
},
srmOpen: function(event) {
$('#SRMWrap').one("webkitTransitionEnd", function() {
$('#SRMenu').removeClass('collapsed').addClass('expanded');
}).removeClass('collapsed').addClass('expanded');
},
srmClose: function(event) {
if ( $('#SRMenu').hasClass('expanded') ) {
$('#SRMenu').one("webkitTransitionEnd", function() {
$('#SRMWrap').removeClass('expanded').addClass('collapsed');
}).removeClass('expanded').addClass('collapsed');
};
},
// === END ====== SIDE RIGHT MENU - SWIPE HANDLER ===================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment