Bootstrap 3 modal vertical position center

后端 未结 30 1239
天涯浪人
天涯浪人 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:48

    $('#myModal').on('shown.bs.modal', function() {
        var initModalHeight = $('#modal-dialog').outerHeight(); //give an id to .mobile-dialog
        var userScreenHeight = $(document).outerHeight();
        if (initModalHeight > userScreenHeight) {
            $('#modal-dialog').css('overflow', 'auto'); //set to overflow if no fit
        } else {
            $('#modal-dialog').css('margin-top', 
            (userScreenHeight / 2) - (initModalHeight/2)); //center it if it does fit
        }
    });
    

提交回复
热议问题