Skip to content

Instantly share code, notes, and snippets.

@carolina-vallejo
Last active August 27, 2016 17:44
Show Gist options
  • Save carolina-vallejo/172f72567e348e5577165e6fcfc98cf4 to your computer and use it in GitHub Desktop.
Save carolina-vallejo/172f72567e348e5577165e6fcfc98cf4 to your computer and use it in GitHub Desktop.
lock/unlock scroll
function lock_scroll(){
// lock scroll position, but retain settings for later
var scrollPosition = [
self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
];
var html = jQuery('html'); // it would make more sense to apply this to body, but IE7 won't have that
html.data('scroll-position', scrollPosition);
html.data('previous-overflow', html.css('overflow'));
html.css('overflow', 'hidden');
window.scrollTo(scrollPosition[0], scrollPosition[1]);
//---safari
document.ontouchmove = function(e){ e.preventDefault(); }
}
function unlock_scroll(){
// un-lock scroll position
var html = jQuery('html');
var scrollPosition = html.data('scroll-position');
html.attr('style','');
//html.css('overflow', html.data('previous-overflow'));
window.scrollTo(scrollPosition[0], scrollPosition[1]);
//--- safari
document.ontouchmove = function(e){ return true; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment