【问题标题】:Center jQuery popUp dialog with auto width and auto height使用自动宽度和自动高度居中 jQuery 弹出对话框
【发布时间】:2013-10-18 04:56:17
【问题描述】:

我有以下 jQuery 弹出对话框的初始化,带有动态内容:

  jQuery("#popUp").live("click", function (e) {
   e.preventDefault();
    jQuery("<div></div>")
        .addClass("dialog")
        .attr("id", "popUpDialog")
        .appendTo("body")
        .dialog({
            title: jQuery(this).attr("data-dialog-title"),
            close: function() { jQuery(this).remove(); },
            modal: true,
            hide: { effect: "none", duration: 150 },
            show: { effect: "none", duration: 150 },
            width: 'auto',
            height: 'auto',
            position: {
                my: 'center',
                at: 'center',
            of: jQuery(window)
            }
        }).load(this.href);

    jQuery(".close").live("click", function (e) {
        e.preventDefault();
        jQuery(this).closest(".dialog").dialog("close");
    });
});

});

对于这个最简单的问题,我很抱歉,但我不能将弹出窗口放在整个页面的中心。问题出在:

                  position: {
                  my: 'center',
                  at: 'center',
              of: jQuery(window)}

【问题讨论】:

  • position: { my: 'center', at: 'center', of: jQuery(window) } 删除它默认它在中心显示对话框

标签: jquery dialog popup position center


【解决方案1】:

问题是因为对话框在其内容加载之前被定位,因此它的大小正在改变。更改您的逻辑,以便首先加载内容,然后实例化对话框:

$('<div />', {
  class: 'dialog',
  id: 'popUpDialog' 
}).appendTo('body').load(this.href, function() {
  // set up the dialog in the callback of the AJAX request...
  $(this).dialog({
    title: jQuery(this).attr("data-dialog-title"),
    close: function() { jQuery(this).remove(); },
    modal: true,
    hide: { effect: "none", duration: 150 },
    show: { effect: "none", duration: 150 },
    width: 'auto',
    height: 'auto',
    position: {
      my: 'center',
      at: 'center',
      of: window
    }
  })
});

【讨论】:

  • 我在我的应用程序(MVC)中经常使用它。单击具有 id 'popUp' 的按钮时,内容将显示在 popUp 中,我如何更改我的逻辑,我可以使用 SeTimeOut功能?
  • 可以,您需要将左侧位置设置为(window.width / 2) - (dialog.width / 2),顶部位置也一样。
【解决方案2】:

这对于位置选项应该足够了:

position:'center'

有效吗?

【讨论】:

  • 不,它不起作用,如果我指出宽度和高度,它会起作用,宽度:自动和高度:自动不起作用......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
相关资源
最近更新 更多