我尝试了以下代码,但它只将对话框的左上角位置定位到中心,这会导致元素与右侧对齐。如何将对话框居中到实际中心,以计算元素的宽度,使得中心线将对话框分成50% 50%的两半?
$('.selector').dialog({ position: 'center' });
http://docs.jquery.com/UI/Dialog#option-position
:这是一个链接,指向jQuery UI对话框的position选项的文档页面。请注意保留HTML标记。
我尝试了以下代码,但它只将对话框的左上角位置定位到中心,这会导致元素与右侧对齐。如何将对话框居中到实际中心,以计算元素的宽度,使得中心线将对话框分成50% 50%的两半?
$('.selector').dialog({ position: 'center' });
http://docs.jquery.com/UI/Dialog#option-position
:$("#myDialog").position({
my: "center",
at: "center",
of: window
});
文档: http://jqueryui.com/demos/position/
获取: http://jqueryui.com/download
$('#myDialog').dialog('widget').position({my:"center", at:"center", of:window})
。 - semmelbroesel$('#myDialog').dialog('widget')
和 $('#myDialog').parent()
都可以使用。 - actaram我相信你不需要设置位置:
$("#dialog").dialog();
我查看了这篇文章,并查看了官方 jquery-ui 网站关于对话框定位的内容,其中讨论了两种状态:初始化和初始化后。
代码示例 - (取自 jQuery UI 2009-12-03)
使用指定的 position 选项初始化对话框。
$('.selector').dialog({ position: 'top' });
在初始化后获取或设置位置选项。
//getter
var position = $('.selector').dialog('option', 'position');
//setter
$('.selector').dialog('option', 'position', 'top');
我认为,如果您删除position属性,您会发现它会自动居中,否则请尝试第二个setter选项,其中定义了"option"、"position"和"center"的3个元素。
由于对话框需要一个位置,因此您需要包含js位置。
<script src="jquery.ui.position.js"></script>
open: function () {
var win = $(window);
$(this).parent().css({
position: 'absolute',
left: (win.width() - $(this).parent().outerWidth()) / 2,
top: (win.height() - $(this).parent().outerHeight()) / 2
});
}
window.scrollTo(0, 0);
对我非常有效 +1 - MaVRoSCyleft: (window.innerWidth - $(this).parent().parent().outerWidth())/2,
top: (window.innerHeight - $(this).parent().parent().outerHeight())/2
- Doctor Parameter所以,如果有人像我一样遇到了这个页面,或者在15分钟后忘记了,我正在使用jqueryui对话框版本1.10.1和jquery 1.9.1与ie8在一个iframe中(blah),它需要指定一个within,否则它不起作用,即:
position: {
my: "center bottom",
at: "center top",
of: $("#submitbutton"),
within: $(".content")
}
感谢 @vm370 指我正确的方向。
within
,但我点赞的原因是它使用了 position 属性,如下所示:$('.selector').dialog({ position: my: "center bottom", at: "center top", of: $("#submitbutton") });
而不是在结尾处添加一个调用:$('.selector').position(...)
。 - LosManosposition: { my: "center bottom", at: "center center", of: window },
在 http://api.jqueryui.com/position/ 的文档中,使用选项 "using" 可能有更准确的定位方式,但我很匆忙...
open : function() {
var t = $(this).parent(), w = window;
t.offset({
top: (w.height() / 2) - (t.height() / 2),
left: (w.width() / 2) - (t.width() / 2)
});
}
我必须调用函数dialog()
两次来定位对话框(jQuery v1.11.2 / jQueryUI v1.10.4)。
$("#myDialog").dialog({
/* initial dialog parameters */
}).dialog({
position: {
my: "center center",
at: "center center",
of: window
}
});
$(window).resize(function() {
$("#dialog").dialog("option", "position", "center");
});
Jquery UI 1.9.2
及以后版本的jquery不支持IE8
我找到了两个解决方案。
回滚到jquery UI 1.7.2
以支持IE8,
尝试使用Jquery UI 1.9.2
的这段代码
position: {my: "center", at: "center", of: $("body"),within: $("body") }
body
的中心?有点奇怪... 如果您的页面是普通网页(即比视口更长),则您的对话框窗口将出现在用户视野之外,他需要向下滚动才能看到它。 - ThePhi