Detect real screen resolution (ignoring browser zoom etc.)

回眸只為那壹抹淺笑 提交于 2019-12-01 17:59:11

问题


I need to get the real screen resolution using JavaScript (client-side at least), but having issues with both IE and Firefox as they tend to change this when you use the browser's built-in zoom.

How can this be done in a cross-browser friendly manner?


回答1:


function getDimension() {
  var width = 0, height = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    height = window.innerHeight;
    width = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    height = document.documentElement.clientHeight;
    width = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    height = document.body.clientHeight;
    width = document.body.clientWidth;
  }
  window.alert( 'Width = ' + width + 'Height = ' + height);
}

or (jquery)

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


来源:https://stackoverflow.com/questions/5342805/detect-real-screen-resolution-ignoring-browser-zoom-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!