如何将 jQuery 对话框定位到中心?
我尝试过以下代码,但它仅将对话框左上角位置定位到中心,这使得元素向右对齐。如何将对话框居中到计算元素宽度的真实中心,以便中心线将对话框切成 50% 50% 的一半?
$('.selector').dialog({ position: 'center' });
I have tried following code, but it only positions dialogs left upper corner position to center, and that makes element to be aligned to right. How can I center dialog to real center which counts elements width, so that center line will cut dialog to 50% 50% halfs?
$('.selector').dialog({ position: 'center' });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
最新的 jQuery UI 有一个位置组件:
Doc: http://jqueryui.com/demos/position/
获取:http://jqueryui.com/download
The latest jQuery UI has a position component:
Doc: http://jqueryui.com/demos/position/
Get: http://jqueryui.com/download
我很确定您不需要设置位置:
默认情况下应居中。
我确实看过这篇文章,还检查了 官方 jquery-ui 网站上的内容关于定位对话框:其中讨论了两种状态:初始化和初始化后。
代码示例 -(摘自 jQuery UI 2009-12-03)
使用指定的位置选项初始化对话框。
初始化后获取或设置位置选项。
我认为,如果您要删除位置属性,您会发现它自行居中,否则请尝试第二个设置器选项,其中您定义了“选项”、“位置”和“中心”的 3 个元素。
I'm pretty sure you shouldn't need to set a position:
should center by default.
I did have a look at the article, and also checked what it says on the official jquery-ui site about positioning a dialog : and in it were discussed 2 states of: initialise and after initialise.
Code examples - (taken from jQuery UI 2009-12-03)
Initialize a dialog with the position option specified.
Get or set the position option, after init.
I think that if you were to remove the position attribute you would find it centers by itself else try the second setter option where you define 3 elements of "option" "position" and "center".
因为对话框需要一个位置,所以需要包含js位置
Because the dialog need a position, You need to include the js position
因此,如果有人像我一样点击此页面,或者当我在 15 分钟内忘记时,我正在使用 iframe 中的 jqueryui 对话框版本 1.10.1 和 jquery 1.9.1 以及 ie8(废话),并且它需要在指定或它不起作用,即
感谢@vm370为我指明了正确的方向。
So if anyone hits this page like I did, or for when I forget in 15 minutes, I'm using jqueryui dialog version 1.10.1 and jquery 1.9.1 with ie8 in an iframe(blah), and it needs a within specified or it doesn't work, i.e.
Thanks to @vm370 for pointing me in the right direction.
我得到了最好的结果,将 jQuery 对话框放在浏览器窗口的中心:
可能有更准确的方法来使用选项“using”来定位它,如 http://api.jqueryui.com/position/ 但我很着急......
I'm getting best results to put jQuery dialog in the center of browser's window with:
There's probably more accurate way to position it with option "using" as described in the documentation at http://api.jqueryui.com/position/ but I'm in a hurry...
为了固定中心位置,我使用:
To fix center position, I use:
试试这个....
Try this....
我必须调用函数
dialog()
两次来定位对话框(jQuery v1.11.2 / jQueryUI v1.10.4)。I have to call function
dialog()
twice to position the dialog (jQuery v1.11.2 / jQueryUI v1.10.4).Jquery UI 1.9.2
,jquery及更高版本不支持IE8我找到了两个解决方案。
回滚到
jquery UI 1.7.2
以支持 IE8,尝试使用此代码
Jquery UI 1.9.2
Jquery UI 1.9.2
, jquery and the later versions don' support IE8I found two solutions for that.
Rollback to
jquery UI 1.7.2
to support IE8,Try this code with
Jquery UI 1.9.2
添加声明
另一件事可能会给您带来 JQuery 对话框定位的麻烦,特别是对于大于浏览器视图端口的文档 - 您必须在文档顶部
。如果没有它,jquery 倾向于将对话框放在页面底部,并且在尝试拖动它时可能会发生错误。
Another thing that can give you hell with JQuery Dialog positioning, especially for documents larger than the browser view port - you must add the declaration
At the top of your document.
Without it, jquery tends to put the dialog on the bottom of the page and errors may occur when trying to drag it.
以下位置参数对我有用
祝你好运!
Following position parameter worked for me
Good luck!
根据下面的讨论,该问题可能是由于较新的 jQuery 版本中的 IE 兼容性较差,恢复到 1.7.2 似乎可以解决该问题,该问题仅出现在 IE8 中。
http://forum.jquery.com /topic/jquery-ui-dialog-positioning-not-working-in-ie-8
According to the following discussion, the problem could be due to less IE compatibility in the newer jQuery versions, reverting back to 1.7.2 seems to resolve the problem, which only occurs in IE8.
http://forum.jquery.com/topic/jquery-ui-dialog-positioning-not-working-in-ie-8
我遇到了这个问题,我终于弄清楚了。直到今天我还在使用 jQuery 的一个非常旧的版本,版本 1.8.2。
在我使用
dialog
的所有地方,我都使用以下位置选项对其进行初始化:但是,我发现删除
position: "center"
或用正确的语法替换它并没有' t 做这个技巧,例如:虽然上面是正确的,但我还在加载页面时使用
option
将位置设置为中心,以旧的方式,如下所示:这导致了所有我的对话框窗口粘在视口的左上角。
我必须用下面正确的新语法替换所有实例:
I had a problem with this and I finally figured this out. Until today I was using a really old version of jQuery, version 1.8.2.
Everywhere where I had used
dialog
I had initialised it with the following position option:However, I found that removing
position: "center"
or replacing it with the correct syntax didn't do the trick, for example:Although the above is correct, I was also using
option
to set the position as center when I loaded the page, in the old way, like so:This was causing all of my dialog windows to stick to the top left of the view port.
I had to replace all instances of this with the correct new syntax below:
如果您使用单独的 jquery 文件或自定义 jquery 下载,请确保您也将 jquery.ui.position.js 添加到您的页面中。
If you are using individual jquery files or a custom jquery download either way make sure you also have jquery.ui.position.js added to your page.
你只在 IE 中遇到这个问题吗?如果是这样,请尝试将其添加到页面 HEAD 标记的第一行:
虽然所有兼容性问题都在后来的 jQueries 中得到了修复,但今天我遇到了这个问题。
Are you running into this in IE only? If so, try adding this to the FIRST line of your page's HEAD tag:
I had though that all compatibility issues were fixed in later jQueries, but I ran into this one today.
对于旧版本和不想使用位置的人尝试此操作:
Try this for older versions and somebody who don't want to use position:
如果在移动设备上使用 jquery ui,您还需要手动重新居中 - 对话框是通过“左和右”手动定位的。顶部'CSS 属性。如果用户切换方向,则定位不再居中,并且之后必须进行调整/重新居中。
You also need to do manual re-centering if using jquery ui on mobile devices - the dialog is manually positioned via a 'left & top' css property. if the user switches orientation, the positioning is no longer centered, and must be adapted / re-centered afterwards.
对于Win7/IE9环境只需在你的css文件中设置:
For Win7/IE9 environment just set in your css file:
我用CSS修复了:
I fixed with css:
无法让 IE9 将对话框居中。
通过将其添加到 css 来修复它:
百分比无关紧要。任何少量的数字都有效。
Could not get IE9 to center the dialog.
Fixed it by adding this to the css:
Percent doesn't matter. Any small number worked.