在页面加载时使用 javascript(无 jquery)自行调整窗口大小并居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14866418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Self resize and center a window using javascript (no jquery) on page load
提问by CMSCSS
This is not something we'd normally do but we're trying load a video into the blank window opened by a clickTag (from a banner ad) - and make it feel like a modal window as much as possible.
这不是我们通常会做的事情,但我们正在尝试将视频加载到由 clickTag(来自横幅广告)打开的空白窗口中 - 并尽可能让它感觉像一个模态窗口。
Is it possible to use javascript to self-resize the blank window opened by the clickTag and center it on the screen?
是否可以使用 javascript 自行调整由 clickTag 打开的空白窗口的大小并将其居中显示在屏幕上?
- We can't apply anything to the link that is clicked so...
- Everything has to self-run as the page loads
- If possible, it would be nice to not see the browser tabs and address bar
- We're not loading jquery
- Are browser security alerts a potential problem?
- 我们无法对点击的链接应用任何内容,所以...
- 一切都必须在页面加载时自行运行
- 如果可能,最好不要看到浏览器选项卡和地址栏
- 我们没有加载 jquery
- 浏览器安全警报是否是潜在问题?
Essentially the user clicks to watch a video and we'd like them to watch the video without them feeling like they've gone to a completely different site.
本质上,用户点击观看视频,我们希望他们在观看视频时不会觉得他们去了一个完全不同的网站。
So far I can resize the window but can't figure out how to center it with:
到目前为止,我可以调整窗口大小,但不知道如何将其居中:
<script language="javascript">
function resizeVideoPage(){
resizeTo(400,390);
window.focus();
}
</script>
// Then...
<body onload="resizeVideoPage();"></body>
Any pointers in the right direction would be much appreciated.
任何指向正确方向的指针将不胜感激。
Cheers
干杯
Ben
本
回答by CMSCSS
Got it working with:
得到它的工作:
<script language="javascript">
function resizeVideoPage(){
var width = 600;
var height = 400;
window.resizeTo(width, height);
window.moveTo(((screen.width - width) / 2), ((screen.height - height) / 2));
}
</script>
// Then...
<body onload="resizeVideoPage();"></body>
回答by winner_joiner
I personally would advice against Popups an Javascript window manipulation. (due to Popup Blockers, Javascript errors, ...) A Overlay would probably be better.
我个人建议反对 Popups 一个 Javascript 窗口操作。(由于弹出窗口阻止程序,Javascript 错误,...)覆盖可能会更好。
Here would be an other Solution.
这将是另一个解决方案。
File one(Link should point to this Helper, that opens the Video HTML)
文件一(链接应指向此助手,打开视频 HTML)
<html>
<body>
<script>
var windowWidth = 400;
var windowHeight = 200;
var xPos = (screen.width/2) - (windowWidth/2);
var yPos = (screen.height/2) - (windowHeight/2);
window.open("popup.html","POPUP","width="
+ windowWidth+",height="+windowHeight +",left="+xPos+",top="+yPos);
</script>
</body>
</html>
File Two (Video that closes Helper)
文件二(关闭助手的视频)
<html>
<body onload="window.opener.close();">
</body>
</html>

