获取文档的宽度和高度
问题描述:
我很好奇我如何获得文档的宽度,它看起来像我可以使用document.body.offsetWidth
获得窗口的宽度,但我尝试的其他所有事情都像是与document.body.offsetWidth
不同的几个像素,我需要更大的文档宽度,并且在窗口大小调整时不会更改。获取文档的宽度和高度
答
function getSize()
{
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0];
var x=w.innerWidth||e.clientWidth||g.clientWidth;
var y=w.innerHeight||e.clientHeight||g.clientHeight;
return {width:x, height:y}
}
var size=getSize();
console.log(size.width+" "+size.height);
这个返回窗口的大小,这就是为什么当我改变的结果框的尺寸值改变,我需要做任何改变时,有一个摄像提升价值2000x3000像素(例如)。 – erikvold 2012-04-27 23:52:19
http://www.howtocreate.co.uk/tutorials/javascript/browserwindow and http://stackoverflow.com/questions/5484578/how-to-get-document-height-and-width-without-using-jquery – 2012-04-28 00:03:10
对于vanilla JavaScript来说+1,不管OP是要求它还是jQuery;我们经常会忘记jQuery如何轻松创建JavaScript。 – Bojangles 2012-04-28 00:54:46