javascript - 如何使用我已有的代码将 javascript 弹出窗口居中

标签 javascript popup window center

我需要能够将出现的弹出窗口居中。我知道这个问题已经被问过很多次了,但我找不到一个有效的。请帮忙!

jQuery(document).ready(function (jQuery) {
       jQuery('.open-window').click(function () {
            var NWin = window.open(jQuery(this).prop('href'), '', 'scrollbars=1,height=400,width=400');
                  if (window.focus) {
                      NWin.focus();
                  }
                  return false;
      });
});

最佳答案

我想这就是你所追求的

jQuery(document).ready(function (jQuery) {
    var screenWidth = (screen.width-400)/2,
        screenHeight = (screen.height-400)/2;

jQuery('.open-window').click(function () {
    var NWin = window.open(jQuery(this).prop('href'), '', "'scrollbars=1, height=400, width=400, top="+screenHeight+", left="+screenWidth+"'");

    if (window.focus) {
        NWin.focus();
    }
    return false;
    });
});

http://jsbin.com/cesitun/edit?html,js,console,output

关于javascript - 如何使用我已有的代码将 javascript 弹出窗口居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33617491/

相关文章:

apache-flex - 弹性 : Cannot tab between controls on a modal popup

mysql - MYSQL8.0.11上窗口函数括号错误

window - 如何从 CLI 获取 KDE 中所有打开的窗口的列表?

javascript - 在 JavaScript 中单击浏览器窗口或选项卡关闭时如何实现注销功能?

javascript - 谷歌翻译(在工具栏中)如何绕过跨站点脚本限制?

javascript - SequelizeDatabaseError : Sending the error in the response

Java GUI 对话框超链接

javascript - angularJS 页面不渲染任何内容

JavaScript。异步函数的执行顺序

popup - (JavaFX) 执行操作时如何显示临时弹出窗口 (OSD)?