Last active
August 29, 2015 14:15
-
-
Save JoanClaret/49e7dede7ef41efe4715 to your computer and use it in GitHub Desktop.
Quojs: Open / Close sidebar navigation by swipe
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 $swipeable = $$('nav'); | |
var first = true; | |
var locked = false; | |
var direction; | |
$swipeable.swipe(function(e) { | |
// after swipe, reinitialize control variables | |
first = true; | |
locked = false; | |
}); | |
$swipeable.swiping(function(e){ | |
if (!locked){ | |
if (first){ | |
// capture initial position | |
initialX = e.touch.x; | |
initialY = e.touch.y; | |
first = false; | |
} | |
else{ | |
// capture next positions | |
var lastX = e.touch.x; | |
var lastY = e.touch.y; | |
// calculate distance between first and last position | |
var difX = Math.abs(initialX - lastX); | |
var difY = Math.abs(initialY - lastY); | |
// if distance is bigger than 5, we got the direction | |
if (difX > 5 || difY > 5 ){ | |
locked = true; | |
} | |
if ( difX > difY){ | |
direction = 'horizontal'; | |
} | |
else if ( difX < difY){ | |
direction = 'vertical'; | |
} | |
else{ | |
// fallback | |
direction = 'unknown'; | |
locked = false; | |
} | |
} | |
}else{ | |
if (direction == 'horizontal'){ | |
// avoid translate layer more distance than layer width | |
if (e.touch.x > $swipeable.offset().width){ | |
return; | |
} | |
x = e.touch.x - $swipeable.offset().width; | |
// move without animation while we are swipping | |
$swipeable.vendor('transition', 'none'); | |
$swipeable.vendor('transform', 'translateX(' + x +'px)'); | |
// once the swipe finishes, re-open or close nav with animation | |
$swipeable.swipe(function(e) { | |
var distance = 0; | |
if (e.touch.x < ($swipeable.offset().width / 2)) | |
{ | |
distance = $swipeable.offset().width*-1; | |
} | |
$swipeable.vendor('transition', 'transform .5s ease'); | |
$swipeable.vendor('transform', 'translateX(' + distance +'px)'); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Developed by David Panyella ( https://github.com/davidpanyella ) i Joan Claret
FULL CODE: http://codepen.io/joanclaret/pen/yyvGvP
DEMO: http://s.codepen.io/joanclaret/debug/yyvGvP