Skip to content

Instantly share code, notes, and snippets.

@potench
Created February 14, 2012 01:08
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 potench/1822234 to your computer and use it in GitHub Desktop.
Save potench/1822234 to your computer and use it in GitHub Desktop.
Tilt / Scroll
(function () {
var min_tilt = 15, // minimum the user must tilt to start the scroll
max_speed = 5, // maximum tilt scrolling speed
gravity : .3, // easing
velocity:0 // current velocity
setScroll = (function (delta) {
if(Math.abs(delta) > min_tilt){ // scroll
var cur_vel = - Math.max(-max_speed, Math.min(max_speed, (delta / min_tilt) * 3));
velocity += ((cur_vel - velocity) * gravity);
// content.position().left += velocity;
}
}),
deviceorientation = null,
orientationchange = (function() {
if(deviceorientation) {
window.removeEventListener("deviceorientation", deviceorientation, true);
}
if(window.orientation%180===0) { // portrait
deviceorientation = (function(e) {
setScroll(e.gamma);
});
}else { // landscape
deviceorientation = (function(e) {
setScroll(e.beta);
});
}
window.addEventListener("deviceorientation",deviceorientation,true);
})();
$(window).bind("orientationchange", orientationchange);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment