在屏幕上定位窗口

10

有没有一种方式可以将用jQuery打开的窗口定位到屏幕的右上角?

这是我目前的代码:

$(document).ready(function() {
    $("#dirs a").click(function() {
        // opens in new window
        window.open(this.href, "customWindow", "width=960, height=1040");
        return false;
    });
});

我希望它可以像在 Windows Aero snap(Vista 或更高版本)中“捕捉”一样,在屏幕的右上角打开。有没有办法实现这个?

顺便说一下,这是一个简单的页面,只有我会使用,并且我只会在分辨率为1920x1080的监视器上使用Chrome浏览器,因此不需要任何适应不同浏览器或屏幕尺寸的花哨功能。

5个回答

21

如果他想要它在右上方,他不需要这个吗?

window.open(this.href, "customWindow", "width=960, height=1040, top=0, left=960");

哦,天啊,只需要添加left=960就解决了。谢谢,太完美了 :) - JacobTheDev
1
抱歉,顶部应该是0,而不是40!已修复。 - nmford

7

JavaScript的window.open可以接受许多参数。对于您特定的情况,top和left就足够了。

请参见Fiddle示例!

语法

window.open([URL], [Window Name], [Feature List], [Replace]);

功能列表

图片描述

适应您需求的工作示例

<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>

注意: 这将计算屏幕宽度以正确设置left

请考虑您使用的窗口具有较大的高度,通常,屏幕比较宽而不是长...


1
$("#dirs a").click(function(e) {
    e.preventDefault();
    var popupWidth = 960;
    var leftPos = screen.width - popupWidth;

    window.open(this.href, "customWindow", "width=" + popupWidth + ", height=1040, top=0, left=" + leftPos);
});

这是一个 IT 相关的内容,翻译如下:

这里有一个示例


1
window.open(this.href, "customWindow", "width=960, height=1040, top=0, left=0");

其他窗口属性:

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

似乎无法工作,至少在Chrome中不行。编辑:在Firefox中可以正常工作。如果我得不到更好的答案,也许我会使用它。 - JacobTheDev
@Marko - 真的吗?!- 只有一个正确答案,而我们同时发布了。 - web_bod

1

这是正确的..您需要先计算屏幕的宽度。

var leftPos = screen.width - 960;
window.open(this.href, "customWindow", "width=960, height=1040, top=40, left="+leftPos+");

1
这是一个简单的页面,只有我会使用,并且我只会在分辨率为1920x1080的Chrome浏览器上使用它。 - nmford

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接