jQuery 在屏幕上定位窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10728207/
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
Position a Window On Screen
提问by JacobTheDev
Is there a way to position a window opened with jQuery to the top right corner of the screen?
有没有办法将用 jQuery 打开的窗口定位到屏幕的右上角?
This is the code I have right now:
这是我现在拥有的代码:
$(document).ready(function() {
$("#dirs a").click(function() {
// opens in new window
window.open(this.href, "customWindow", "width=960, height=1040");
return false;
});
});
And I want it to open in the top right corner of the screen, similar to how it would appear if "snapped" with Windows Aero snap in Vista or higher. Is there a way to make this happen?
而且我希望它在屏幕的右上角打开,类似于在 Vista 或更高版本中使用 Windows Aero snap“捕捉”时的显示方式。有没有办法做到这一点?
By the way, this is a simple page that only I will use, and I will only use it in Chrome on a 1920x1080 monitor, so it doesn't have to have any fancy stuff to adjust for different browsers or screen sizes.
顺便说一下,这是一个只有我会使用的简单页面,而且我只会在 Chrome 中的 1920x1080 显示器上使用它,因此它不必有任何花哨的东西来针对不同的浏览器或屏幕尺寸进行调整。
回答by nmford
If he wants it on the top right doesn't he need this?
如果他想要它在右上角,他不需要这个吗?
window.open(this.href, "customWindow", "width=960, height=1040, top=0, left=960");
回答by Zuul
JavaScript window.open accepts lots of parameters. To your particular case, top and left should suffice.
JavaScript window.open 接受大量参数。对于您的特定情况,顶部和左侧就足够了。
See the working Fiddle Example!
请参阅工作小提琴示例!
The Syntax
语法
window.open([URL], [Window Name], [Feature List], [Replace]);
The Feature list
功能列表
The working example to fit your needs
满足您需求的工作示例
<script type="text/javascript">
<!--
function popup(url)
{
var width = 960;
var height = 1040;
var left = screen.width - 960;
var top = 0;
var params = 'width='+width+', height='+height;
params += ', top='+top+', left='+left;
params += ', directories=no';
params += ', location=no';
params += ', menubar=no';
params += ', resizable=no';
params += ', scrollbars=no';
params += ', status=no';
params += ', toolbar=no';
newwin=window.open(url,'customWindow', params);
if (window.focus) {newwin.focus()}
return false;
}
// -->
</script>
<a href="javascript: void(0)"
onclick="popup('popup.html')">Top Right popup window</a>
Note:This will calculate the screen width to set the left
properly.
注意:这将计算屏幕宽度以left
正确设置。
Take into consideration that you are using a window with a large height, usually, screens are larger than taller...
考虑到您使用的是高度较大的窗口,通常,屏幕大于高度...
回答by Marko
回答by web_bod
window.open(this.href, "customWindow", "width=960, height=1040, top=0, left=0");
Other window properties:
其他窗口属性:
Property Default value Description
width auto specifies width of the new window in pixels
height auto height of the window in pixels
top auto specifies window position
left auto specifies window position
directories no should the directories bar be shown? (Links bar)
location no specifies the presence of the location bar
resizable no specifies whether the window can be resized.
menubar no specifies the presence of the menu bar
toolbar no specifies the presence of the toolbar
scrollbars no specifies the presence of the scrollbars
status no specifies the presence of the statusbar
回答by Gijoey
This is the right one.. You need to calculate the width of the screen first.
这是正确的..您需要先计算屏幕的宽度。
var leftPos = screen.width - 960;
window.open(this.href, "customWindow", "width=960, height=1040, top=40, left="+leftPos+");