Skip to content

Instantly share code, notes, and snippets.

@alexandr-kazakov
Last active April 10, 2017 18:24
Show Gist options
  • Save alexandr-kazakov/240fd37b69984426af6a207476096d82 to your computer and use it in GitHub Desktop.
Save alexandr-kazakov/240fd37b69984426af6a207476096d82 to your computer and use it in GitHub Desktop.
relative modal block centering Vanilla JS
function modalCentering(modal_arg) {
var modal = modal_arg;
modal.style.top = document.documentElement.clientHeight / 2 - modal.offsetHeight / 2 + 'px';
modal.style.left = document.documentElement.clientWidth / 2 - modal.offsetWidth / 2 + 'px';
if (modal.getBoundingClientRect().top < 0) modal.style.top = 0;
window.addEventListener('resize', function() {
modal.style.top = document.documentElement.clientHeight / 2 - modal.offsetHeight / 2 + 'px';
modal.style.left = document.documentElement.clientWidth / 2 - modal.offsetWidth / 2 + 'px';
if (modal.getBoundingClientRect().top < 0) modal.style.top = 0;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment