检测窗口宽度并补偿滚动条 - Javascript [英] detect window width and compensate for scrollbars - Javascript

查看:137
本文介绍了检测窗口宽度并补偿滚动条 - Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Javascript和滚动条帐户检测用户窗口的宽度? (我需要滚动条内屏幕的宽度)。这是我的...它似乎在多个浏览器中工作...除了它没有考虑滚动条..

How do I detect the width of a user's window with Javascript and account for their scrollbar? (I need the width of the screen INSIDE of the scrollbar). Here's what I have...it seems to work in multiple browsers...except that it doesn't account for the scrollbars..

    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;
}

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

any ideas? i need it to work in all browsers;)

推荐答案

您将找到哪些浏览器支持哪些属性的大摘要<在quirksmode.org上的这个页面上有一个href =http://www.quirksmode.org/dom/w3c_cssom.html =noreferrer>。

You will find the big summary of what properties are supported on what browsers on this page on quirksmode.org.

你最好的选择是抓住页面中的一个元素(使用document.body支持,或者document.getElementById或其他),走它的offsetParent链来找到最顶层的元素,然后检查该元素的clientWidth和clientHeight。

Your best bet is probably to grab an element in the page (using document.body where supported, or document.getElementById or whatever), walk its offsetParent chain to find the topmost element, then examine that element's clientWidth and clientHeight.

这篇关于检测窗口宽度并补偿滚动条 - Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆