Javascript 不同窗口大小的jQuery弹出位置

Javascript 不同窗口大小的jQuery弹出位置,javascript,jquery,Javascript,Jquery,我想使用JavaScript控制不同大小窗口的弹出位置 我如何做到?< p>控制不同窗口大小的弹出对话框窗口使用窗口的宽度和对话框的宽度来计算对话框的位置,例如:如果你需要对话框出现在屏幕的中间,不管窗口宽度如何,试试这个: var windowWidth = $(window).width(); //get the windows width; var dialogWidth = $("#popup-dialog").width(); // get the dialog width; var

我想使用JavaScript控制不同大小窗口的弹出位置


我如何做到?

< p>控制不同窗口大小的弹出对话框窗口使用窗口的宽度和对话框的宽度来计算对话框的位置,<强>例如:如果你需要对话框出现在屏幕的中间,不管窗口宽度如何,试试这个:

var windowWidth = $(window).width(); //get the windows width;
var dialogWidth = $("#popup-dialog").width(); // get the dialog width;
var pLeft = (windowWidth - dialogWidth) / 2;
$("#dialog").css("left", pLeft);

我假设你在说标准的
窗口。打开
弹出窗口,如果没有请告诉我们

你要做的很简单。首先使用
屏幕
对象找出客户的屏幕尺寸:

var screenWidth = screen.width;
var screenHeight = screen.height;
现在我们有了屏幕大小,我们可以使用它来计算800x600尺寸的中心弹出窗口的左上角:

var top = screenWidth/2 - 300;
var left = screenHeight/2 - 400;
现在,我们可以开始了:

var myCenteredWindow = window.open(myURL,'myFancyPopup','width=800,height=600,scrollbars=no,top=' + top + ',left=' + left + '');
请注意,这可以很容易地写在一行中(当您了解发生的情况时):


你能再具体一点吗?看看这个参考资料:
var myCenteredWindow = window.open(myURL,'myFancyPopup','width=800,height=600,scrollbars=no,top=' + screen.width/2 - 300 + ',left=' + screen.height/2 - 400 + '');