How to auto-center jQuery UI dialog when resizing browser?

前端 未结 6 1248
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 07:44

When you use jquery UI dialog, all works well, except for one thing. When the browser is resized, the dialog just stays in it\'s initial position which can be really annoyin

6条回答
  •  失恋的感觉
    2020-12-04 08:34

    Setting the position option will force this, so just use the same selector covering all your dialogs where I use #dialog here (if it doesn't find them no action is taken, like all jQuery):

    jQuery UI before 1.10

    $(window).resize(function() {
        $("#dialog").dialog("option", "position", "center");
    });
    

    jQuery UI 1.10 or higher

    $(window).resize(function() {
        $("#dialog").dialog("option", "position", {my: "center", at: "center", of: window});
    });
    

    Here's that same jQuery UI demo page adding only the code above, we're just adding a handler to the window's resize event with .resize(), so it triggers the re-center at the appropriate time. ​

提交回复
热议问题