jQuery 对话框始终居中

发布于 2024-12-06 20:04:31 字数 630 浏览 1 评论 0原文

我如何实现一个具有自动宽度和宽度的 jQuery 模式对话框?高度始终在浏览器中居中。调整浏览器窗口大小后也是如此。

下面的代码不起作用。我认为问题在于自动宽度和宽度高度。

jQuery - 代码

$("<div class='popupDialog'>Loading...</div>").dialog({
  autoOpen: true,
  closeOnEscape: true,
  height: 'auto',
  modal: true,
  title: 'About Ricky',
  width: 'auto'
}).bind('dialogclose', function() {
  jdialog.dialog('destroy');
}).load(url, function() {
  $(this).dialog("option", "position", ['center', 'center'] );
});

$(window).resize(function() {
  $(".ui-dialog-content").dialog("option", "position", ['center', 'center']);
});

谢谢!

How can I implement, that a jQuery modal dialog with auto width & height is always centered in the browser. Also after resizing the window of the browser.

The following code doesn't work. I think the problem is the auto width & height.

jQuery - code

$("<div class='popupDialog'>Loading...</div>").dialog({
  autoOpen: true,
  closeOnEscape: true,
  height: 'auto',
  modal: true,
  title: 'About Ricky',
  width: 'auto'
}).bind('dialogclose', function() {
  jdialog.dialog('destroy');
}).load(url, function() {
  $(this).dialog("option", "position", ['center', 'center'] );
});

$(window).resize(function() {
  $(".ui-dialog-content").dialog("option", "position", ['center', 'center']);
});

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃不饱 2024-12-13 20:04:32

实际上,我认为 position: ["center", "center"] 不是最好的解决方案,因为它分配了显式的 top:left:< /code> 根据创建时视口的大小向对话框添加 css 属性。

当我尝试在屏幕上垂直放置对话框中心时,我遇到了同样的问题。这是我的解决方案:

.dialog() 函数的 options 部分,我传递 position:[null, 32]null 设置元素样式的 left: 值,32 只是为了使对话框固定在窗口顶部。还要确保设置明确的宽度。

我还使用 dialogClass 选项分配一个自定义类,它只是一个 margin:0 auto; css 属性:

.myClass{
    margin:0 auto;
}

我的对话框如下所示:

  $('div#popup').dialog({
    autoOpen: false,
    height: 710,
    modal: true,
    position: [null, 32],
    dialogClass: "myClass",
    resizable: false,
    show: 'fade',
    width: 1080
  });

希望这对某人有帮助。

Acutally, I think the position: ["center", "center"] not be the best solution, as it assigns an explict top: and left: css properties to the dialog based on the size of the viewport at creation.

I came across this same issue when trying to have a dialog center on screen vertically. Here is my solution:

In the options part of my .dialog() function, I pass position:[null, 32]. The null sets the left: value the element's style, and the 32 is just for keeping the dialog box pegged to the top of the window. Also be sure to set an explicit width.

I also use the dialogClass option to assign a custom class, which is simply a margin:0 auto; css property:

.myClass{
    margin:0 auto;
}

And my dialog looks like:

  $('div#popup').dialog({
    autoOpen: false,
    height: 710,
    modal: true,
    position: [null, 32],
    dialogClass: "myClass",
    resizable: false,
    show: 'fade',
    width: 1080
  });

Hopefully this helps someone.

恬淡成诗 2024-12-13 20:04:32
$(window).resize(function() {
    $("#dialog").dialog("option", "position", "center");
});

工作 jsFiddle: http://jsfiddle.net/vNB8M/

对话框以自动宽度和宽度居中。高度并在窗口调整大小后继续居中。

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

Working jsFiddle: http://jsfiddle.net/vNB8M/

The dialog is centered with auto width & height and continues to be centered after window resize.

败给现实 2024-12-13 20:04:32

所有答案都没有达到我想要的效果。对于那些仍然遇到问题的人,这对我有用。这将强制对话框始终显示在屏幕中央,并且当浏览器调整大小时,它将使对话框居中。

$( "#ShowDialogButton" ).click(function(){
    $( "#MyDialog" ).dialog({
       show: 'fade'
     }).dialog( "option", "position", { my: "center", at: "center", of: window } );

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

});

None of the answers did quite what I wanted. For those that are still having problems with this, here is what worked for me. This will force the dialog to always appear in the center of the screen, and it will center the dialog as the browser is resized.

$( "#ShowDialogButton" ).click(function(){
    $( "#MyDialog" ).dialog({
       show: 'fade'
     }).dialog( "option", "position", { my: "center", at: "center", of: window } );

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

});
甜是你 2024-12-13 20:04:32

对话框周围的模态窗口应该允许您将对话框放置在以下 css 的中心:

margin-left:auto;margin-right:auto;

这不起作用吗?您有我们可以查看的“示例”页面吗?

The modal window around the dialog box should allow you to place the dialog box centered with the css of:

margin-left:auto;margin-right:auto;

Does this not work? Do you have a 'sample' page that we can look at?

燕归巢 2024-12-13 20:04:32

此解决方案有效:

$(window).resize(function () 
{
    $("#myDialog").dialog("close");
    $("#myDialog").dialog("open");
});

性能相当糟糕,但您可以等待调整大小结束: jQuery - 如何等待“resize”事件的“结束”然后才执行操作?

我也尝试过这个,但我无法滚动到低于或高于打开对话框的元素的位置:

$(window).scroll(function () 
{
    $("#myDialog").dialog("close");
    $("#myDialog").dialog("open");
});

This solution is working:

$(window).resize(function () 
{
    $("#myDialog").dialog("close");
    $("#myDialog").dialog("open");
});

Performance is quite bad, but you can wait for resize end: jQuery - how to wait for the 'end' of 'resize' event and only then perform an action?.

I also tried this, but I can't scroll lower or higher than position of element which opened dialog:

$(window).scroll(function () 
{
    $("#myDialog").dialog("close");
    $("#myDialog").dialog("open");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文