如何在窗口调整大小后获取高度和宽度

5
我有一个Scorm模块,它在我的js文件中使用window.open在新窗口中打开并设置了resizable = 1:
function OpenScormModuleWindow(scormModuleId, scormModuleUrl, title, width, height) 
{
  _scormModuleId = scormModuleId;
  InitializeUserScormModule();
  scormModuleWindow = window.open(scormModuleUrl, "title", "width=" + width + ", height=" + height + ", resizable = 1");
  scormModuleWindow.focus();
}

在我看来,我有:

  var myWidth = 0, myHeight = 0;
  if (typeof (window.innerWidth) == 'number')
  {
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
      myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
      myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;
  }

我能获取高度和宽度,但是不正确。

现在用户可以调整大小,当窗口大小正确时,如何获取高度和宽度?

我需要获取这些值,以便保存它,下次打开窗口时它的大小是正确的。

谢谢。

3个回答

12

使用一个监听 resize 事件的事件监听器,然后你就可以重置你的值:

window.addEventListener('resize', setWindowSize);

function setWindowSize() {
  if (typeof (window.innerWidth) == 'number') {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else {
    if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    } else {
      if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }
    }
  }
}

如果您需要一个跨浏览器的解决方案,请使用 jQuery ($.on('resize', func)) 或参见 JavaScript window resize event


1
你需要调用一个匿名函数来调用setWindowSize吗?难道你不能写成“window.addEventListener('resize', setWindowSize);”吗? - chris5marsh

3
希望这个例子能对您有所帮助... HTML 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Window Size : height and width</title>
</head>
<!-- Resize the window (here output panel) and see the result !-->
<body onload="getSize()" onresize="getSize()">
<div id="wh"> 
<!-- Place height and width size here! --> 
</div>
<body>
</body>
</html>

JavaScript代码:

function getSize()
{
var w = document.documentElement.clientWidth;
var h = document.documentElement.clientHeight;

// put the result into a h1 tag
 document.getElementById('wh').innerHTML = "<h1>Width: " + w + " Height: " + h + "</h1>";
}

或者您也可以使用jQuery代码进行调整大小:

var height = $(window).height();
var width = $(window).width();

$(window).resize(function() {

var height = $(window).height();
var width = $(window).width();
console.log("height"+height);
console.log("width"+width);

});

0

就我所见,这是最好的方法;

function resizeCanvas() {
    canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

    WIDTH = canvas.width;
    HEIGHT = canvas.height;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接