18

如何使用 Javascript 检测用户窗口的宽度并考虑他们的滚动条?(我需要滚动条内的屏幕宽度)。这就是我所拥有的......它似乎可以在多个浏览器中工作......除了它不考虑滚动条......

    function browserWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

有任何想法吗?我需要它在所有浏览器中工作;)

4

7 回答 7

7

如果您只对宽度感兴趣,一个(非常讨厌的)解决方法是创建一个 1px x 100% div 并使用它的 offsetWidth。适用于 IE>=7、FF、Chrome、Safari 和 Opera(我没有尝试过 IE6,因为我们正在努力让你很幸运,它可以正常工作,所以不要抱怨-关于这些天的渲染奇数政策)。我用 attributes 将 div 挂在 document.body 上{ position: 'absolute', top: '-1px', left: 0, width: '100%', height: '1px' },在第一次需要时创建它。

如果你能忍受它就可以工作。

于 2010-12-13T18:05:49.437 回答
5

您将在 quirksmode.org 的此页面上找到哪些浏览器支持哪些属性的概要。

您最好的选择可能是抓取页面中的一个元素(在支持的情况下使用 document.body 或 document.getElementById 或其他),遍历其 offsetParent 链以找到最顶层的元素,然后检查该元素的 clientWidth 和 clientHeight。

于 2009-02-27T18:57:10.233 回答
3

只需在 window.innerWidth() 检查之前添加:

if (typeof(document.body.clientWidth) == 'number') {
    // newest gen browsers
    width = document.body.clientWidth;
    height = document.body.clientHeight;
}
于 2011-08-04T10:41:41.393 回答
3

This is what I did - only half a year into learning JavaScript, so this may be a bad fix. First, I created a transparent square image (10px x 10px), but you could also create a non-transparent image and add this to your JavaScript as
document.getElementById('getDimensions').style.visibility = "hidden";

HTML:

<img id="getDimensions" src="images/aSmallBox.png" alt="A small transparent image  
meant to determine the dimensions of the viewport" width="10px" height="10px"/>

JavaScript:

//Inside function called by window.onload event handler (could go in CSS file, but   
//better to keep it with other related code in JS file)
document.getElementById('getDimensions').style.position = "fixed";
document.getElementById('getDimensions').style.bottom = "0px";
document.getElementById('getDimensions').style.right = "0px";   

//Everything below inside function called by window.onresize event handler
var baseWidthCalculation = document.getElementById('getDimensions').offsetLeft;
var baseHeightCalculation = document.getElementById('getDimensions').offsetTop;
  //Account for the dimensions of the square img element (10x10)
var viewportWidth = baseWidthCalculation + 10;
var viewportHeight = baseHeightCalculation + 10;
于 2012-02-17T05:28:26.413 回答
3

This is a more efficient version of an idea posted here by Tim Salai ( even though you are just beginning, it was brilliant to place an element in the bottom right corner like that ).

var getDimensions = document.createElement("div");
getDimensions.setAttribute("style", 
            "visibility:hidden;position:fixed;bottom:0px;right:0px;");
document.getElementsByTagName("body")[0].appendChild(getDimensions);
var viewportWidth = getDimensions.offsetLeft;
var viewportHeight = getDimensions.offsetTop;

And here is a modular version

var PageDimensions = function(){
 var Width;
 var Height;     

 function pagedimensionsCtor (){
   var getDimensions = document.createElement("div");
   getDimensions.setAttribute("style" , 
         "visibility:hidden;position:fixed;bottom:0px;right:0px;");
   document.getElementsByTagName("body")[0].appendChild(getDimensions);
   Width = getDimensions.offsetLeft;
   Height = getDimensions.offsetTop;
   getDimensions.parentNode.removeChild(getDimensions);
 }
 pagedimensionsCtor();

 function Reset(){
  pagedimensionsCtor();
 }     

 function GetPageHeight(){
  return Height;
 }

 function GetPageWidth(){
  return Width;
 }

 return{
  Reset: Reset,
  GetPageHeight: GetPageHeight,
  GetPageWidth: GetPageWidth
 };
}

Use the modular version:

var page = new PageDimensions();
console.log("viewportWidth: " + page.GetPageWidth() + " viewportHeight: " + page.GetPageHeight());

Bonus feature:

page.Reset();//just in case you think your dimensions have changed
于 2012-11-11T22:33:53.500 回答
0

我会将“innerWidth”与身体的宽度进行比较。如果 body width > innerwidth,则存在滚动条。

if (browserWidth() < document.body.offsetWidth) {
  doSomething();
}
于 2009-02-27T18:59:36.717 回答
0

Another solution you can try is changing some CSS so scrolling happens within your page, instead of the browser window doing it. In the style for body, add overflow:auto. Now the body element includes the scrollbar, so when you get the window width you're measuring the width outside the scrolling container instead of inside it.

This does feel like a potential source of quirkiness, and possibly an accessibility issue, so if you're going for widespread use you might want to test it quite carefully.

于 2011-12-21T05:59:29.720 回答