Re-center Bootstrap Modal Vertically After Content Has Loaded
Solution 1:
According to the docs there is a method called loaded.bs.modal
where the event '...is fired when the modal has loaded content using the remote option.'
So with your code it would be something like this:
$('#myModal').on('loaded.bs.modal', function () {
var contentHeight = $(window).height() - 60;
var headerHeight = $(this).find('.modal-header').outerHeight() || 2;
var footerHeight = $(this).find('.modal-footer').outerHeight() || 2;
});
Here's a fork of that PEN that should work (haven't tested with remote source) http://codepen.io/craigmdennis/pen/fChIm
Update based on comments.
Its showing the modal before it calculates the width & height, then centers it once it's got them. You can't get dimensions from a hidden object as they don't have any until they're displayed. You'll have to add a class to the modal markup so you can set visibility: hidden;
and z-index: -99;
(so it's invisible and behind any content so not clickable) then remove the class when the loaded.bs.modal
event is fired.
In the CSS:
.invisible {
visibility: hidden;
position: absolute; /* It will already have a position declaration */z-index: -99;
}
Then in the JavaScript:
$('#myModal').on('loaded.bs.modal', function () {
var contentHeight = $(window).height() - 60;
var headerHeight = $(this).find('.modal-header').outerHeight() || 2;
var footerHeight = $(this).find('.modal-footer').outerHeight() || 2;
$(this).removeClass('invisible');
});
Post a Comment for "Re-center Bootstrap Modal Vertically After Content Has Loaded"