Skip to content

Instantly share code, notes, and snippets.

@ggendre
Last active December 11, 2015 17:18
Show Gist options
  • Save ggendre/4633210 to your computer and use it in GitHub Desktop.
Save ggendre/4633210 to your computer and use it in GitHub Desktop.
/*
Use this to enable divs with overflow:scroll to scroll in android <3 web views.
Useful for phonegap apps.
Be sure to test for device first.
Usage example (needs jquery or zepto lib):
if (android_2){
enableTouchScroll( $('#myCrollableContent'), { x: true, y: true } );
}
*/
function enableTouchScroll(selector, axis){
if ( ! axis ) axis = { x : false, y : true }
var scrollStartPosTop = 0;
var scrollStartPosLeft = 0;
$(selector).live('touchstart', function( event ){
if ( axis.y ) scrollStartPosTop = this.scrollTop + event.originalEvent.touches[0].pageY;
if ( axis.x ) scrollStartPosLeft = this.scrollLeft + event.originalEvent.touches[0].pageX;
});
$(selector).live('touchmove', function( event ){
if ( axis.y ) this.scrollTop = scrollStartPosTop - event.originalEvent.touches[0].pageY;
if ( axis.x ) this.scrollLeft = scrollStartPosLeft - event.originalEvent.touches[0].pageX;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment