Skip to content

Instantly share code, notes, and snippets.

@sundaycrafts
Created May 23, 2015 06:16
Show Gist options
  • Save sundaycrafts/af0f9ed28de87d496b66 to your computer and use it in GitHub Desktop.
Save sundaycrafts/af0f9ed28de87d496b66 to your computer and use it in GitHub Desktop.
prevent pull to refresh mobile chrome feature
/**
* inspired by jdduke (http://jsbin.com/qofuwa/2/edit)
*/
var preventPullToRefresh = (function preventPullToRefresh(lastTouchY) {
lastTouchY = lastTouchY || 0;
var maybePrevent = false;
function setTouchStartPoint(event) {
lastTouchY = event.touches[0].clientY;
// console.log('[setTouchStartPoint]TouchPoint is ' + lastTouchY);
}
function isScrollingUp(event) {
var touchY = event.touches[0].clientY,
touchYDelta = touchY - lastTouchY;
// console.log('[isScrollingUp]touchYDelta: ' + touchYDelta);
lastTouchY = touchY;
// if touchYDelta is positive -> scroll up
if(touchYDelta > 0){
return true;
}else{
return false;
}
}
return {
// set touch start point and check whether here is offset top 0
touchstartHandler: function(event) {
if(event.touches.length != 1) return;
setTouchStartPoint(event);
maybePrevent = window.pageYOffset === 0;
// console.log('[touchstartHandler]' + maybePrevent);
},
// reset maybePrevent frag and do prevent
touchmoveHandler: function(event) {
if(maybePrevent) {
maybePrevent = false;
if(isScrollingUp(event)) {
// console.log('======Done preventDefault!======');
event.preventDefault();
return;
}
}
}
}
})();
// usage
// document.addEventListener('touchstart', preventPullToRefresh.touchstartHandler);
// document.addEventListener('touchmove', preventPullToRefresh.touchmoveHandler);
@nghiepdev
Copy link

nghiepdev commented Dec 20, 2017

Hi,

Your code has conflicted with -webkit-overflow-scrolling: touch; on iOS when scrolling to end container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment