在網上看到有Bootstrap2的Modal dialog垂直居中問題解決方法,這種方法本身試了一下,並不能徹底居中,而且窗口的大小不同的話,每次顯示的margin-top值也會改變,遮蓋層還會出現滾動條,效果也很差看,代碼以下:javascript
- //在初始顯示時設置垂直居中
- $('#YourModal').modal().css({
- 'margin-top': function () {
- return -($(this).height() / 2);
- }
- });
- //或者咱們能夠將這個效果註冊到顯示事件中:
- $('.modal').on('show', function() {
- $(this).css({
- 'margin-top': function () {
- return -($(this).height() / 2);
- }
- });
- });
今天正好遇到這個問題,不過我用的Bootstrap框架是Bootstrap3版本了,解決代碼以下: css
- $('#YourModal').on('show.bs.modal', function (e) {
- $(this).find('.modal-dialog').css({
- 'margin-top': function () {
- var modalHeight = $('#yourModal').find('.modal-dialog').height();
- return ($(window).height() / 2 - (modalHeight / 2));
- }
- });
- });