Skip to content

Instantly share code, notes, and snippets.

@yangshun
Last active August 29, 2015 14:21
Show Gist options
  • Save yangshun/1b3b3639449b6fa67963 to your computer and use it in GitHub Desktop.
Save yangshun/1b3b3639449b6fa67963 to your computer and use it in GitHub Desktop.
/*!
jQuery.touchScrollBuster v0.0.1
Tay Yang Shun https://github.com/yangshun
The MIT License (MIT)
Copyright (c) 2015
$(el).touchScrollBuster() will prevent parent containers from scrolling
when the child container is at the top or at the bottom.
*/
(function($) {
var lockTop = false;
var lockBottom = false;
var lastY = 0;
$.fn.touchScrollBuster = function () {
$(this).on('touchstart' , function (e) {
var target = e.currentTarget;
var currentScrollPos = $(target).scrollTop();
lockTop = false;
lockBottom = false;
if (currentScrollPos === 0) {
lockTop = true;
lastY = e.originalEvent.touches[0].clientY;
} else if (currentScrollPos === target.scrollHeight - target.offsetHeight) {
lockBottom = true;
lastY = e.originalEvent.touches[0].clientY;
}
});
$(this).on('touchmove' , function (e) {
var currentY = e.originalEvent.touches[0].clientY;
if ((lockTop && (currentY > lastY)) ||
(lockBottom && (currentY < lastY))) {
e.preventDefault();
}
});
}
}(window.jQuery || window.Zepto));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment