10

有没有办法将使用 jQuery 打开的窗口定位到屏幕的右上角?

这是我现在拥有的代码:

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

而且我希望它在屏幕的右上角打开,类似于在 Vista 或更高版本中使用 Windows Aero snap“捕捉”时的显示方式。有没有办法做到这一点?

顺便说一句,这是一个只有我会使用的简单页面,而且我只会在 1920x1080 显示器上的 Chrome 中使用它,因此它不需要任何花哨的东西来调整不同的浏览器或屏幕尺寸。

4

5 回答 5

21

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

window.open(this.href, "customWindow", "width=960, height=1040, top=0, left=960");
于 2012-05-23T21:40:05.573 回答
7

JavaScript window.open 接受很多参数。对于您的特定情况,顶部和左侧就足够了。

请参阅工作小提琴示例!

语法

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正确设置。

考虑到您使用的是高度较大的窗口,通常屏幕大于高度...

于 2012-05-23T21:47:32.473 回答
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);
});

这是一个例子

于 2012-05-23T21:37:19.700 回答
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
于 2012-05-23T21:38:16.933 回答
1

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

var leftPos = screen.width - 960;
window.open(this.href, "customWindow", "width=960, height=1040, top=40, left="+leftPos+");
于 2012-05-23T21:42:03.863 回答