Skip to content

Instantly share code, notes, and snippets.

@round
Last active August 29, 2015 14:05
Show Gist options
  • Save round/dfd1fff43256c6b9f1ff to your computer and use it in GitHub Desktop.
Save round/dfd1fff43256c6b9f1ff to your computer and use it in GitHub Desktop.
Prevent Overscrolling on iOS
//uses document because document will be topmost level in bubbling
$(document).on('touchmove',function(e){
e.preventDefault();
});
//uses body because jquery on events are called off of the element they are
//added to, so bubbling would not work if we used document instead.
$('body').on('touchstart','.scrollable',function(e) {
if (e.currentTarget.scrollTop === 0) {
e.currentTarget.scrollTop = 1;
} else if (e.currentTarget.scrollHeight === e.currentTarget.scrollTop + e.currentTarget.offsetHeight) {
e.currentTarget.scrollTop -= 1;
}
});
//prevents preventDefault from being called on document if it sees a scrollable div
$('body').on('touchmove','.scrollable',function(e) {
e.stopPropagation();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment