UI对话框上的自动高度,如果高度更改对话框不再垂直居中

时间:2013-07-29 08:41:28

标签: jquery jquery-ui-dialog

我有以下函数将URL加载到我的对话框中,当打开该URL时,函数在文档就绪时运行,该函数根据值显示或隐藏div。反过来扩展对话框,自动高度工作,但对话框向下扩展,不再居中。

所以Dialog Open>在对话框中打开的URL> show panel run>对话框扩展高度>对话框不再居中。

代码试用自动重新进入中心

$.ui.dialog.prototype.options.autoReposition = true;
$(window).resize(function () {
    $(".editDialog").dialog("option", "position", "center");
    $(".ui-dialog-content:visible").each(function () {
        $(this).dialog("option", "position", $(this).dialog("option", "position"));
    });
});
$(".ui-dialog-content").resize(function () {
    $(".ui-dialog-content:visible").each(function () {
        $(this).dialog("option", "position", $(this).dialog("option", "position"));
    });
});

对话代码

$(document).ready(function () {
    var width = $(window).width() / 2;
    $.ajaxSetup({ cache: false });

$(".editDialog").live("click", function (e) {
        var url = $(this).attr('href');
        $("#dialog-edit").dialog({
            title: 'Edit',
            autoOpen: false,
            resizable: true,
            autoResize:true,
            minHeight: 'auto',
            width: width,
            modal: true,
            draggable: true,
            open: function (event, ui) {
                //Show the loading div on open.
                $("#dvLoading").show();
                //adding a callback function wich will be launched after the loading
                $(this).load(url,function(response, status, xhr) {
                    if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        $(this).html(msg + xhr.status + " " + xhr.statusText);
                    } else $("#dvLoading").hide();
                });
            },
            close: function (event, ui) {
                $(this).dialog('close');
            }
        });

        $("#dialog-edit").dialog('open');
        return false;
    });

URL文档准备显示/隐藏DIVS

$(document).ready(function () {
    showPanel()
});
function showPanel()
{
    var panel = $("#lstAssetTypes").val();
    panel = panel.substr(panel.indexOf(",") + 1)

    switch (panel)
    {
        case "User":
            $("#dvWKS").show();
            $("#dvNTW").hide();

            break;
        case "Network":
            $("#dvWKS").hide();
            $("#dvNTW").show();
            break;
        default:
            break;
    }
}

Divs的HTML称为

 <div id="dialog-confirm" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
        Are you sure to delete?
    </p>
</div>
<div id="dialog-edit" style="display: none">
    <div id="dvLoading" class="loading"><img src="~/Images/loading.gif"><p>Loading please wait...</p></div>
</div>
<div id="dialog-view" style="display: none">
</div>

2 个答案:

答案 0 :(得分:2)

似乎无法自动检测高度变化,没有事件发生。相反,您需要手动调用重定位代码。使用这样的函数:

var reposition_dialog = function() {
    $("#dialog-edit").dialog("option", "position", "center");
        $(".ui-dialog-content:visible").each(function () {
        $(this).dialog("option", "position", $(this).dialog("option", "position"));
    });
}

(我将.editDialog从您的代码更改为#dialog-edit,因为它似乎是一个错误)

完成更改内容后,只需调用该函数:

$(document).ready(function () {
    showPanel()
});
function showPanel()
{
    var panel = $("#lstAssetTypes").val();
    panel = panel.substr(panel.indexOf(",") + 1)

    switch (panel)
    {
        case "User":
            $("#dvWKS").show();
            $("#dvNTW").hide();

            break;
        case "Network":
            $("#dvWKS").hide();
            $("#dvNTW").show();
            break;
        default:
            break;
    }
    reposition_dialog();
}

如果你真的想自动检测它,我只会因某些原因无法预测内容何时会发生变化,请看一下这个帖子: Detect element content changes with jQuery

答案 1 :(得分:0)

尝试以下方法:

$.fn.center = function () {
    this.css("position","absolute");
    this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + 
                                            $(window).scrollTop()) + "px");
    this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + 
                                            $(window).scrollLeft()) + "px");
    return this;
}

$(dialogID).center();

// calling that function again on resize of window
$(window).resize(function(){
   $(dialogID).center();
});

// or you can either call it whenever your dialog is updated with new URL etc etc.