Skip to content

Instantly share code, notes, and snippets.

@JoanClaret
Created February 11, 2015 14:44
Show Gist options
  • Save JoanClaret/5ae0fa74cf456d61aa39 to your computer and use it in GitHub Desktop.
Save JoanClaret/5ae0fa74cf456d61aa39 to your computer and use it in GitHub Desktop.
touchSwipe.js: Open / Close sidebar navigation by swipe
var NAV_WITH = 300;
var speed = 200;
var swipeOptions = {
triggerOnTouchEnd: true,
swipeStatus: swipeStatus,
allowPageScroll: "vertical",
threshold: 100
};
$(function () {
nav = $("nav");
nav.swipe(swipeOptions);
});
/**
* Catch each phase of the swipe.
* move : we drag the div
* cancel : we animate back to where we were
* end : we animate to the next image
*/
function swipeStatus(event, phase, direction, distance) {
if (phase == "move" && (direction == "left")) {
var duration = 0;
scrollNav(distance, duration);
} else if (phase == "cancel") {
scrollNav(0, speed);
} else if (phase == "end") {
hideNav();
}
}
function hideNav() {
scrollNav(NAV_WITH, speed);
}
/**
* Manually update the position of the nav on drag
*/
function scrollNav(distance, duration) {
nav.css("transition-duration", (duration / 1000).toFixed(1) + "s");
//inverse the number we set in the css
var value = (distance < 0 ? "" : "-") + Math.abs(distance).toString();
nav.css("transform", "translate(" + value + "px,0)");
}
/**
* Open it again
*/
$('a').click(function(){
scrollNav(0, speed);
});
@JoanClaret
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment