获取屏幕,当前网页和浏览器窗口的大小

如何获取在全部主流浏览器中均可以使用的windowWidthwindowHeightpageWidthpageHeightscreenWidthscreenHeightpageXpageYscreenXscreenYjavascript

屏幕截图描述了所需的值


#1楼

这包含您须要了解的全部信息: 获取视口/窗口大小 css

简而言之: html

var win = window,
    doc = document,
    docElem = doc.documentElement,
    body = doc.getElementsByTagName('body')[0],
    x = win.innerWidth || docElem.clientWidth || body.clientWidth,
    y = win.innerHeight|| docElem.clientHeight|| body.clientHeight;
alert(x + ' × ' + y);

小提琴 java

请中止编辑此答案。 如今已经由不一样的人对其进行了20次编辑,以匹配他们的代码格式首选项。 还指出了,若是您只想定位现代浏览器,则不须要这样作-若是是这样,则只须要如下内容: shell

const width  = window.innerWidth || document.documentElement.clientWidth || 
document.body.clientWidth,
const height = window.innerHeight|| document.documentElement.clientHeight|| 
document.body.clientHeight;

console.log(width, height);

#2楼

一种获取可用屏幕尺寸的非jQuery方法。 window.screen.width/height已经设置好了,可是出于响应性网页设计和完整性的考虑,我认为值得一提的是这些属性: 浏览器

alert(window.screen.availWidth);
alert(window.screen.availHeight);

http://www.quirksmode.org/dom/w3c_cssom.html#t10app

availWidthavailHeight-屏幕上的可用宽度和高度(不包括OS任务栏等)。 dom


#3楼

您还能够获取WINDOW的宽度和高度,避免使用浏览器工具栏和其余东西。 这是浏览器窗口中实际可用的区域工具

为此,请使用: window.innerWidthwindow.innerHeight属性( 请参阅w3schools的文档 )。 测试

例如,在大多数状况下,这是显示完美居中浮动模式对话框的最佳方法。 不管使用哪一种分辨率方向或窗口大小,均可以使用它来计算窗口的位置。


#4楼

这是使用纯JavaScript的跨浏览器解决方案( ):

var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

#5楼

有时您须要在调整窗口和内部内容的大小时看到宽度/高度的变化。

为此,我编写了一个小脚本,其中添加了一个对话框,该对话框能够动态监视全部调整大小并当即进行更新。

它添加了具备固定位置和高z-index的有效HTML,可是足够小,所以您能够

  • 实际站点上使用
  • 用它来测试移动/响应视图


通过测试:Chrome 40,IE11,但也极可能也能够在其余/旧版浏览器上工做... :)

function gebID(id){ return document.getElementById(id); }
  function gebTN(tagName, parentEl){ 
     if( typeof parentEl == "undefined" ) var parentEl = document;
     return parentEl.getElementsByTagName(tagName);
  }
  function setStyleToTags(parentEl, tagName, styleString){
    var tags = gebTN(tagName, parentEl);
    for( var i = 0; i<tags.length; i++ ) tags[i].setAttribute('style', styleString);
  }
  function testSizes(){
    gebID( 'screen.Width' ).innerHTML = screen.width;
    gebID( 'screen.Height' ).innerHTML = screen.height;

    gebID( 'window.Width' ).innerHTML = window.innerWidth;
    gebID( 'window.Height' ).innerHTML = window.innerHeight;

    gebID( 'documentElement.Width' ).innerHTML = document.documentElement.clientWidth;
    gebID( 'documentElement.Height' ).innerHTML = document.documentElement.clientHeight;

    gebID( 'body.Width' ).innerHTML = gebTN("body")[0].clientWidth;
    gebID( 'body.Height' ).innerHTML = gebTN("body")[0].clientHeight;  
  }

  var table = document.createElement('table');
  table.innerHTML = 
       "<tr><th>SOURCE</th><th>WIDTH</th><th>x</th><th>HEIGHT</th></tr>"
      +"<tr><td>screen</td><td id='screen.Width' /><td>x</td><td id='screen.Height' /></tr>"
      +"<tr><td>window</td><td id='window.Width' /><td>x</td><td id='window.Height' /></tr>"
      +"<tr><td>document<br>.documentElement</td><td id='documentElement.Width' /><td>x</td><td id='documentElement.Height' /></tr>"
      +"<tr><td>document.body</td><td id='body.Width' /><td>x</td><td id='body.Height' /></tr>"
  ;

  gebTN("body")[0].appendChild( table );

  table.setAttribute(
     'style',
     "border: 2px solid black !important; position: fixed !important;"
     +"left: 50% !important; top: 0px !important; padding:10px !important;"
     +"width: 150px !important; font-size:18px; !important"
     +"white-space: pre !important; font-family: monospace !important;"
     +"z-index: 9999 !important;background: white !important;"
  );
  setStyleToTags(table, "td", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
  setStyleToTags(table, "th", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");

  table.style.setProperty( 'margin-left', '-'+( table.clientWidth / 2 )+'px' );

  setInterval( testSizes, 200 );

编辑:如今样式仅适用于记录器表元素-不适用于全部表-这也是一个无jQuery的解决方案:)

相关文章
相关标签/搜索