Created
May 23, 2015 06:16
-
-
Save sundaycrafts/af0f9ed28de87d496b66 to your computer and use it in GitHub Desktop.
prevent pull to refresh mobile chrome feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Your code has conflicted with
-webkit-overflow-scrolling: touch;
on iOS when scrolling to end container.