开发者

Centering a Javascript pop-up window

I want to center a pop-up window within a browser. However, I'd like it to work regardless of the browser size and the browsers position within the users screen.

Position within a browser is straightforward, for example (with jQuery) :

var left = ($(window).width()/2)-(width/2);
..
uwin = window.open(...., left=...);

etc. But if the users browser is not full screen, th开发者_如何学JAVAen it positions relative to full screen rather than browser.

I can get screen using

screen.width()

but that will just position on screen so that's no better.

Anyone ?


You'll need to use both the window size and the browser position on the screen to figure out this information. You can get these from the window.screenTop and window.screenLeft properties. So, to get the the positioning you want, you would first get the position relative to the browser window:

var windowLeft = ($(window).width()/2)-(width/2);

and then you add the offset:

var screenLeft = windowLeft + window.screenLeft;

Then you can do the same for the height.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜