Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kweij/5a0eac4e68e80950c937b6eca10681cb to your computer and use it in GitHub Desktop.
Save kweij/5a0eac4e68e80950c937b6eca10681cb to your computer and use it in GitHub Desktop.
JavaScript-only fix for the background scrolling in Bootstrap modals on iOS (Safari)
let previousScrollY = 0;
$(document).on('show.bs.modal', () => {
previousScrollY = window.scrollY;
$('html').addClass('modal-open').css({
marginTop: -previousScrollY,
overflow: 'hidden',
left: 0,
right: 0,
top: 0,
bottom: 0,
position: 'fixed',
});
}).on('hidden.bs.modal', () => {
$('html').removeClass('modal-open').css({
marginTop: 0,
overflow: 'visible',
left: 'auto',
right: 'auto',
top: 'auto',
bottom: 'auto',
position: 'static',
});
window.scrollTo(0, previousScrollY);
});
@sicjr
Copy link

sicjr commented Apr 7, 2019

Thank you!!

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