Created
April 8, 2013 14:44
-
-
Save frederfred/5337313 to your computer and use it in GitHub Desktop.
Disable negative scroll in iOS
This file contains 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
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(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!