调整大小后如何获得对话框的高度和宽度?

时间:2012-02-18 17:55:39

标签: jquery jquery-ui dialog modal-dialog jquery-ui-dialog

我使用resize enable成功创建了一个对话框但是对于我的项目,我需要在用户调整大小后打开对话框的高度和宽度。

我创建了一个id = opener的按钮和一个id = dialog的div。有人可以帮助我。

使用Javascript:

// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() 
{
    $( "#dialog" ).dialog(
    {
        modal:true,
        autoOpen: false,
        show: "blind",
        hide: "explode",
        buttons: [
        {
            text: "Ok",
            click: function() { $(this).dialog("close"); }
        }] ,
        resizable: true,
        width:'auto',
        height:'auto'
    });

    $( "#opener" ).click(function() 
    {
        $( "#dialog" ).dialog( "open" );
        return false;
    });
});

HTML:

<body>
    <div class="demo">
        <div id="dialog" title="Basic dialog">
            <p>My content here. I want to show the height and width of my dialog after it is resized by a user
            </p>    
        </div>
        <button id="opener">Open Dialog</button>
    </div>
</body>

1 个答案:

答案 0 :(得分:8)

使用resizeStop事件,如下所示:

$( "#dialog" ).dialog({
    modal:true,
    autoOpen: false,
    show: "blind",
    hide: "explode",
    buttons: [{
             text: "Ok",
             click: function() { $(this).dialog("close"); }
             }] ,
    resizable: true,
    width:'auto',
    height:'auto',
    resizeStop: function(event, ui) {
        alert("Width: " + $(this).outerWidth() + ", height: " + $(this).outerHeight());        
    }
});

调整对话框大小后,将触发resizeStop选项中指定的函数内容。