jQuery jQuery打开以固定大小居中的新窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13272086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:27:08  来源:igfitidea点击:

jQuery open new window centered with fixed size

jqueryhtmlpopupnew-window

提问by John

Possible Duplicate:
Center a popup window on screen?

可能的重复:
在屏幕上居中弹出窗口?

I have 4 different links, all of which need to open a new window which will target 4 different html files.

我有 4 个不同的链接,所有这些链接都需要打开一个新窗口,该窗口将针对 4 个不同的 html 文件。

When the links are clicked, it needs to open the html file in question in a new window, both:

单击链接时,需要在新窗口中打开有问题的 html 文件,包括:

  • Centered
  • Fixed size 900 x 600
  • 居中
  • 固定尺寸 900 x 600

I have found this below, but it doesnt seem to cater for centering of the window

我在下面找到了这个,但它似乎不适合窗口的居中

http://jquerybyexample.blogspot.com/2012/05/open-link-in-new-tab-or-new-popup.html

http://jquerybyexample.blogspot.com/2012/05/open-link-in-new-tab-or-new-popup.html

Cheers

干杯

回答by adeneo

To center the new window, give it a left and top value half of the screen size - half of the window size :

要使新窗口居中,请为其指定屏幕大小的一半 - 窗口大小的一半:

var left  = ($(window).width()/2)-(900/2),
    top   = ($(window).height()/2)-(600/2),
    popup = window.open ("", "popup", "width=900, height=600, top="+top+", left="+left);

FIDDLE

小提琴