js 控制打开网页窗口的大小位置

1. 打开窗口即最大化

self.moveTo(0,0)
self.resizeTo(screen.availWidth,screen.availHeight)

2. 自定义窗口方法

// url String 域名
// title String 标题
// w String 宽度
// h String 高度

export default function openWindow(url,title,w,h) {
      //Fixes dual-screen position                             Most browsers        Firefox
      const dualScreenLeft =
        window.screenLeft !== undefined ? window.screenLeft : screen.left;
        const dualScreenTop =
        window.screenTop !== undefined ? window.screenTop : screen.top;

      const width = window.innerWidth
        ? window.innerWidth
        : document.documentElement.clientWidth
        ? document.documentElement.clientWidth
        : screen.width;
      const height = window.innerHeight
        ? window.innerHeight
        : document.documentElement.clientHeight
        ? document.documentElement.clientHeight
        : screen.height;

      const left = width / 2 - w / 2 + dualScreenLeft;
      const top = height / 2 - h / 2 + dualScreenTop;

      const newwindow = window.open(
        url,
        title,
        `toolbar=no, scrollbars=no, resizable=yes, menubar=no, status=no, location=no, center=yes, copyhistory=no ,width=${w}, height=${h}, left=${left}, top=${top}`
      );

      // Puts focus on the newWindow
      if (window.focus) {
        newwindow.focus();
      }

      //   window.open 弹出新窗口的命令;
      //   'page.html' 弹出窗口的文件名;
      //   'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替;
      //   height=100 窗口高度;
      //   width=400 窗口宽度;
      //   top=0 窗口距离屏幕上方的象素值;
      //   left=0 窗口距离屏幕左侧的象素值;
      //   toolbar=no 是否显示工具栏,yes为显示;
      //   menubar,scrollbars 表示菜单栏和滚动栏。
      //   resizable=no 是否允许改变窗口大小,yes为允许;
      //   location=no 是否显示地址栏,yes为允许;
      //   status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;
}

posted @ 2022-04-25 13:47  sk-xm  阅读(875)  评论(0编辑  收藏  举报