在屏幕上放置一个窗口

时间:2012-05-23 21:33:58

标签: jquery resize window position

有没有办法将使用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快照“快照”时的显示效果。有没有办法让这种情况发生?

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

5 个答案:

答案 0 :(得分:19)

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

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

答案 1 :(得分:6)

JavaScript window.open接受大量参数。对于您的特定情况,顶部和左侧应该足够了。

查看工作Fiddle Example!

语法

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

功能列表

enter image description here

满足您需求的工作示例

<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

考虑到您使用的是一个高度较大的窗户,通常,屏幕比较大的......

答案 2 :(得分: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);
});

这是一个example

答案 3 :(得分: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

答案 4 :(得分:1)

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

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