我想将我的 jQuery 对话框定位在远离浏览器右边框的 x 像素处。这有可能吗?
http://jqueryui.com/demos/dialog/
位置选项似乎没有那种设置,但是还有其他方法吗?
这使对话框 div 保持在固定位置
这在 IE FF chrome 和 safari 中对我有用
jQuery("#dialogDiv").dialog({
autoOpen: false,
draggable: true,
resizable: false,
height: 'auto',
width: 500,
modal: false,
open: function(event, ui) {
$(event.target).parent().css('position', 'fixed');
$(event.target).parent().css('top', '5px');
$(event.target).parent().css('left', '10px');
}
});
当你想打开对话框时,只需调用
$('#dialogDiv').dialog('open');
如果你制作你的对话框position:absolute
,那么它就相当于常规的页面流,你可以使用left
andtop
属性将它放置在页面上的任何位置。
$('.selector').dialog({ dialogClass: 'myPosition' });
并将 myPosition css 类定义为:
.myPosition {
position: absolute;
right: 200px; /* use a length or percentage */
}
您可以设置top
、left
、right
和bottom
属性以myPosition
使用像素或百分比等长度。
大多数这些答案对我来说似乎都是解决方法,我想找到官方的 jQuery 方法来做到这一点。通读.position()
文档后,发现确实可以在 jQuery 小部件的初始化中完成:
$("#dialog").dialog({
title:title,
position:{my:"right top",at:"right+100 top+100", of:"body"},
width:width,
height:height
})
其中 +100 分别是与右侧和顶部的距离
我知道答案已经被接受,但以防万一有人需要更多信息: http ://salman-w.blogspot.co.uk/2013/05/jquery-ui-dialog-examples.html
$(function() {
$("#dialog").dialog({
position: {
my: "right bottom",
at: "right bottom",
of: window
}
});
});
使用此代码,您可以指定您的顶部和左侧位置:
$('#select_bezeh_window').dialog({
modal:true,
resizable:false,
draggable:false,
width:40+'%',
height:500 ,
position:{
using: function( pos ) {
$( this ).css( "top", 10+'px' );
$( this ).css( "left", 32+'%' );
}
}
});
看这里:http: //jqueryui.com/demos/dialog/#option-position
使用指定的位置选项初始化对话框。
$('.selector').dialog({ position: 'top' });
在 init 之后获取或设置位置选项。
//getter
var position = $('.selector').dialog('option', 'position');
//setter
$('.selector').dialog('option', 'position', 'top');
这对我来说可以在右上角以 10px 偏移量显示对话框position: "right-10 top+10"
::
$( "#my-dialog" ).dialog({
resizable: false,
height:"auto",
width:350,
autoOpen: false,
position: "right-10 top+10"
});
要固定中心位置,我使用:
open : function() {
var t = $(this).parent(), w = window;
t.offset({
top: (w.height() / 2) - (t.height() / 2),
left: (w.width() / 2) - (t.width() / 2)
});
}