简体   繁体   English

如何获得bootstrap模态大小

[英]How to get bootstrap modal size

I have a bootstrap modal whose size is set by: 我有一个bootstrap模式,其大小设置为:

<div class="modal-dialog modal-lg">

I want to be able to determine the modal's size (width actually) before I make a post request to a PHP program to display some dynamic content before displaying the modal. 我希望能够在向PHP程序发出请求之前确定模态的大小(实际宽度),以便在显示模式之前显示一些动态内容。 Does anyone know how to get this information? 有谁知道如何获取这些信息?

I have also been trying to find either the width or height of the entire modal as well as the modal classes and found this solution. 我也一直试图找到整个模态的宽度或高度以及模态类,并找到了这个解决方案。 Though I must add that with this approach the width and height of the modal are only found after it has loaded. 虽然我必须通过这种方法添加它,但模态的宽度和高度只有加载才能找到。

I think that it is not possible to get the correct width and height of the modal before it is being displayed since it is hidden by default before the user clicks to open it. 我认为这是不可能的,因为它默认情况下,用户点击打开它之前隐藏正在显示它之前获得模态的正确宽度和高度。 style="display: none; as inline style under the modal class in Bootstrap 3.3.2. style="display: none;作为Bootstrap 3.3.2中模态类下的内联样式。

However if you like to get the correct width and height of modal once it is displayed you can use this approach. 但是,如果您想显示模态的正确宽度和高度,则可以使用此方法。

    // once the modal has loaded all calculations can start
    // since the modal is hidden before it is loaded there are
    // no dimesions that can be calculated unfortunately
    $('#myModal').on('shown.bs.modal', function () {


        // get the viewport height
        var viewportHeight = $(window).height();
        console.log ('viewportHeight = ' + viewportHeight);


        // get the viewport width
        var viewportWidth = $(window).width();
        console.log ('viewportWidth = ' + viewportWidth);


        // get the .modal-dialog height and width
        // here the .outerHeight() method has to be used instead of the .height() method
        // see https://api.jquery.com/outerwidth/ and
        // https://api.jquery.com/outerheight/ for reference

        // also the .find() method HAS to be used, otherwise jQuery won't find
        // the correct element, this is why you got strange values beforehand
        var modalDialogHeight = $(this).find('.modal-dialog').outerHeight(true);
        console.log ('modalDialogHeight = ' + modalDialogHeight);

        var modalDialogWidth = $(this).find('.modal-dialog').outerWidth(true);
        console.log ('modalDialogWidth = ' + modalDialogWidth);


        // I have included a simple function to log the width and height
        // of the modal when the browser window is being resized
        var modalContentWidthHeight = function () {

            var modalContentWidth = $('#myModal').find('.modal-content').outerWidth(true);
                console.log ('modalContentWidth = ' + modalContentWidth);

            var modalContentHeight = $('#myModal').find('.modal-content').outerHeight(true);
                console.log ('modalContentHeight = ' + modalContentHeight);     
            };

        $(window).resize(modalContentWidthHeight);

    });

I hope the above code somehow helps you figure out the modal dimensions and that you can take it from there.. 我希望上面的代码以某种方式帮助你弄清楚模态尺寸,你可以从那里拿走它。

Another thing that was really bugging me that you might encounter when using the Bootstrap 3.3.2 modal. 另一件事,真的我,你可能会使用引导3.3.2模式时遇到的问题。 If you like to get rid of this bug Open modal is shifting body content to the left #9855 concerning the modal position and given this bug it still not fixed Modify scrollbar check, stop static nav shift #13103 you can use this approach that works regardless of if you have a vertical scrollbar shown or not. 如果你想摆脱这个错误打开模态正在将身体内容转移到左侧#9855有关模态位置并给出此错误它仍然没有修复修改滚动条检查,停止静态导航班次#13103你可以使用这种方法,无论如何如果您显示或不显示垂直滚动条。

Reference: Bootstrap 3.3.2 Center modal on all viewport sizes with or without vertical scrollbar 参考: Bootstrap 3.3.2所有视口尺寸的中心模态,带或不带垂直滚动条

$('#myModal').on('shown.bs.modal', function () {        

// meassure the padding-right given by BS and apply it as padding-left to the modal
// like this equal padding on either side of the modal is given regardless of media query
var modalPaddingRight = $('#myModal').css('padding-right');
console.log ('modalPaddingRight = ' + modalPaddingRight);


// apply the padding value from the right to the left
$('#myModal').css('padding-left', modalPaddingRight);
console.log ( 
        'modalPaddingLeft = ' + $('#myModal').css('padding-left') +
        ' modalPaddingRight = ' + $('#myModal').css('padding-right')
        );


// apply equal padding on window resize
var modalPaddingLeft = function () {

    var modalPaddingRight = $('#myModal').css('padding-right');
    console.log ('modalPaddingRight = ' + modalPaddingRight);

    $('#myModal').css('padding-left', modalPaddingRight);
    console.log ( 
        'modalPaddingLeft = ' + $('#myModal').css('padding-left') +
        'modalPaddingRight = ' + $('#myModal').css('padding-right')
        );
};

$(window).resize(modalPaddingLeft);
});

I hope some of this answer can help you. 我希望这个答案可以帮助你。 Since I am new to jQuery there might be better or more elegant ways to actually code this, though I would not know how at this stage. 由于我是jQuery的新手,可能有更好或更优雅的方法来实际编写代码,尽管我不知道在这个阶段如何。 Nevertheless I think the information given here might be of help to you. 不过,我认为这里给出的信息对您有所帮助。

If you want the actual dimensions of an element using Javascript, JQuery has the built in .width() and .height() functions. 如果你想使用Javascript的元素的实际尺寸,JQuery有内置的.width().height()函数。 I modified your <div> to add a data- attribute that has the bootstrap class incase you want to access that and an ID for easier access: 我修改了你的<div>来添加一个data-属性,该data-属性包含你想要访问的引导类和一个ID,以便于访问:

<div id="my_modal" class="modal-dialog modal-lg" data-size="modal-lg">

Then access it via Javascript: 然后通过Javascript访问它:

var width = $("#my_modal").width();
var height = $("#my_modal").height();
var size = $("#my_modal").attr("data-size");
console.log("Width Is: " + width + " and Height Is:" + height + "and Size Is:" + size);

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM