Skip to content

Instantly share code, notes, and snippets.

@ismailbaskin
Created March 4, 2016 14:39
Show Gist options
  • Save ismailbaskin/4437fcde2863ca839b33 to your computer and use it in GitHub Desktop.
Save ismailbaskin/4437fcde2863ca839b33 to your computer and use it in GitHub Desktop.
repress chrome mobile pull to refresh
window.addEventListener('load', function() {
var maybePreventPullToRefresh = false;
var lastTouchY = 0;
document.addEventListener('touchstart', function(e) {
if (e.touches.length != 1) return;
lastTouchY = e.touches[0].clientY;
// Pull-to-refresh will only trigger if the scroll begins when the
// document's Y offset is zero.
maybePreventPullToRefresh = window.pageYOffset == 0;
}, false);
document.addEventListener('touchmove', function(e) {
var touchY = e.touches[0].clientY;
var touchYDelta = touchY - lastTouchY;
lastTouchY = touchY;
if (maybePreventPullToRefresh) {
// To suppress pull-to-refresh it is sufficient to preventDefault the
// first overscrolling touchmove.
maybePreventPullToRefresh = false;
if (touchYDelta > 0) {
e.preventDefault();
return;
}
}
}, false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment