Bootstrap 3 modal vertical position center

后端 未结 30 1175
天涯浪人
天涯浪人 2020-11-28 00:15

This is a two part question:

  1. How can you position the modal vertically in the center when you don\'t know the exact height of the modal?

  2. Is

30条回答
  •  [愿得一人]
    2020-11-28 00:52

    it's not that complicated.

    please try this:

    $(document).ready(function(){
        var modalId = "#myModal";
        resize: function(){
                var new_margin = Math.ceil(($(window).height() - $(modalId).find('.modal-dialog').height()) / 2);
                $(modalId).find('.modal-dialog').css('margin-top', new_margin + 'px');
        }
        $(window).resize(function(){
            resize();
        });
        $(modalId).on('shown.bs.modal', function(){
            resize();
        });
    });
    

提交回复
热议问题