Forked from claviska/vertically-centered-bootstrap-modals.js
Created
June 24, 2016 11:08
-
-
Save sni10/9a2125cdd807c39b50822c216b26a356 to your computer and use it in GitHub Desktop.
Vertically Centered Bootstrap Modals
This file contains hidden or 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
/** | |
* Vertically center Bootstrap 3 modals so they aren't always stuck at the top | |
*/ | |
$(function() { | |
function reposition() { | |
var modal = $(this), | |
dialog = modal.find('.modal-dialog'); | |
modal.css('display', 'block'); | |
// Dividing by two centers the modal exactly, but dividing by three | |
// or four works better for larger screens. | |
dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2)); | |
} | |
// Reposition when a modal is shown | |
$('.modal').on('show.bs.modal', reposition); | |
// Reposition when the window is resized | |
$(window).on('resize', function() { | |
$('.modal:visible').each(reposition); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment