Skip to content

Instantly share code, notes, and snippets.

@frederfred
Created April 8, 2013 14:44
Show Gist options
  • Save frederfred/5337313 to your computer and use it in GitHub Desktop.
Save frederfred/5337313 to your computer and use it in GitHub Desktop.
Disable negative scroll in iOS
disableNegativeTouchScroll: function() {
var initialY = null;
var nodeStack = [];
var $window = $(window);
$window.bind('touchstart', function(e) {
initialY = e.originalEvent.pageY;
nodeStack = $(e.target).parents().andSelf().get().reverse();
nodeStack = nodeStack.map(function(node) {
return $(node);
});
});
$window.bind('touchend touchcancel', function(e) {
initialY = null;
nodeStack = [];
});
$window.bind('touchmove', function(e) {
if (!initialY) {
e.preventDefault();
}
var direction = e.originalEvent.pageY - initialY;
for (var i = 0; i < nodeStack.length; i +=1) {
var $node = nodeStack[i];
var nodeHeight = $node.height();
var scrollHeight = $node[0].scrollHeight - 2;
var nodeScrollTop = $node.scrollTop();
if (scrollHeight > nodeHeight) {
// the user is dragging the content up, and the element is already scrolled down a bit.
var allowedUp = direction > 0 && nodeScrollTop > 0;
// the user is dragging the content down, and the element is up a bit.
var allowedDown = direction < 0 && nodeScrollTop < scrollHeight - nodeHeight;
if (allowedUp || allowedDown) {
return;
}
}
}
// disable drag
e.preventDefault();
});
}
@13twelve
Copy link

Thanks!

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