弹出窗口,中心屏幕

its*_*el0 9 google-chrome-extension

我正在使用以下代码在Google Chrome扩展程序中打开弹出窗口,我的一个问题是,如何在用户屏幕的中心打开弹出窗口?

 <script>
  chrome.browserAction.onClicked.addListener(function() {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2); 
  chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': 440, 'height': 220, 'left': '+left+', 'top': '+top+', } , function(window) {
   });
});
  </script>
Run Code Online (Sandbox Code Playgroud)

我也试过这个,结果没有运气.

  <script>
  chrome.browserAction.onClicked.addListener(function() {
  chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': 440, 'height': 220, 'left': (screen.width/2)-(w/2), 'top': (screen.height/2)-(h/2), } , function(window) {
   });
});
  </script>
Run Code Online (Sandbox Code Playgroud)

ser*_*erg 14

当你var obj = {property: value}在JS中看到结构时,它就是一个对象创建.在您的代码中,您尝试将包含窗口属性的对象传递给chrome.windows.create()函数.

正确的代码应该是:

chrome.browserAction.onClicked.addListener(function() {
    var w = 440;
    var h = 220;
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2); 

    chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': w, 'height': h, 'left': left, 'top': top} , function(window) {
    });
});
Run Code Online (Sandbox Code Playgroud)

  • chrome.windows.create 期望 'top' 和 'left' 是整数,因此在传递参数之前确保 Math.round()。 (2认同)