Skip to content

Instantly share code, notes, and snippets.

@zerosignalproductions
Created January 28, 2014 23:32
Show Gist options
  • Save zerosignalproductions/8678834 to your computer and use it in GitHub Desktop.
Save zerosignalproductions/8678834 to your computer and use it in GitHub Desktop.
Add swipe support to websites. Not very generic at the moment.
(function($, window) {
var self = $('html'),
dragDelta = 500;
friction = 0.35;
mouseDown = false,
touchDelta = 0;
startSwipe = 0,
isDragging = false;
//Assume no touch on load
self.addClass('no-touch');
//Start the mouse delta on mouse down
//For this event, calculate the X-axis difference
self.on('touchstart.mobileWeb', function(e) {
if(!$('body').hasClass('nav-open')) {
return;
}
self.removeClass('no-touch');
touchDelta = e.clientX || e.originalEvent.changedTouches[0].pageX;
startSwipe = 0;
mouseDown = true;
});
self.on('mousemove.mobileWeb', function(e) {
if(!$('body').hasClass('nav-open')) {
return;
}
self.addClass('no-touch');
});
self.on('touchmove.mobileWeb', function(e) {
if(!$('body').hasClass('nav-open')) {
return;
}
if(mouseDown) {
isDragging = true;
(startSwipe == 0) ? startSwipe = Date.now() : 0;
tempTouchDelta = Math.abs(touchDelta - (e.clientX || e.originalEvent.changedTouches[0].pageX));
//Allow page scrolling for Android if touchDeltaX is greater than 15 pixels
if(tempTouchDelta > 7) {
e.preventDefault();
e.stopPropagation();
}
}
});
self.on('touchend.mobileWeb', function(e) {
if(!$('body').hasClass('nav-open')) {
return;
}
if(tempTouchDelta <=7) { isDragging = false; mouseDown = false; return; }
if(isDragging) {
touchDelta = touchDelta - (e.clientX || e.originalEvent.changedTouches[0].pageX);
console.log(touchDelta > 30 && $('body').hasClass('nav-open'));
if(touchDelta > 30 && $('body').hasClass('nav-open')) {
$('.site-content-cntr').trigger('click');
} else if(touchDelta < -30) {
//$('.mobile-nav.furl-trigger').trigger('click');
}
isDragging = false;
}
mouseDown = false;
});
}(jQuery, window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment