为了账号安全,请及时绑定邮箱和手机立即绑定

整理了scrollWidth offsetWidth clientWidth innerWidth,不同浏览器输出结果不一样,实际应用中怎么办?

这些属性在不同浏览器下输出结果不一样,实际应用中怎么处理兼容问题?

 <script type="text/javascript">
  var w=document.documentElement.scrollWidth
    ||document.body.scrollWidth;
var h=document.documentElement.scrollHeight
    ||document.body.scroolHeight;
    document.write("scroll输出:"+w+":"+h+"<br/>")
    
var w=document.documentElement.offsetWidth
    ||document.body.offsetWidth;
var h=document.documentElement.offsetHeight
    ||document.body.offsetHeight;
    document.write("offset输出:"+w+":"+h+"<br/>");
    
var w=document.documentElement.clientWidth
      ||document.body.clientWidth;
var h=document.documentElement.clientHeight
      ||document.body.clientHeight;    
    document.write("client输出:"+w+":"+h+"<br/>");
    
var w=window.innerWidth
      ||document.body.innerWidth;
var h=document.documentElement.innerHeight
      ||window.innerHeight;    
    document.write("inner 输出:"+w+":"+h+"<br/>");
    
</script>
<p>
firefox浏览器下输出<br/>
scroll输出:370:614<br/>
offset输出:370:38<br/>
client输出:370:614<br/>
inner 输出:370:614<br/>
</p>
<p>
Edge浏览器下输出<br/>
scroll输出:370:8<br/>
offset输出:370:35<br/>
client输出:370:662<br/>
inner 输出:370:662<br/>
</p>
<!--
scrollWidth offsetWidth 关于网页内容 clientWidth innerWidth可见区
==================================================================
//网页内容的高度和宽度
获取网页内容宽度:document.body.scrollWidth
                 ||document.documentElement.scrollWidth;
获取网页内容高度:document.body.scrollHeight
                 ||document.documentElement.scrollHeight;
-----------------------------------------------------------------                 
//网页内容的高度和宽度(包括滚动条等边线,会随窗口的显示大小改变)
获取网页内容宽度:document.body.offsetWidth
                 ||document.documentElement.offsetWidth;
获取网页内容高度:document.body.offsetHeight
                 ||document.documentElement.offsetHeight;
==================================================================
//HTML文档所在窗口的当前高度和宽度(offsetHeight=scrollHeight+滚动条+ 边框?)                 
获取网页可见区域宽:document.body.clientWidth;
                 ||document.documentElement.clientWidth;
获取网页可见区域高:document.body.clientHeight;
                 ||document.documentElement.clientHeight;
-----------------------------------------------------------------                
//获得浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)
获取网页可见区域宽:window.innerWidth;             
获取网页可见区域高:window.innerHeight;
               
注:同一浏览器下innerHeight与clientHeight的输出结果一样,但在不同的浏览器有不同的输出结果,兼容性差。
====================================================================
屏幕分辨率的宽: window.screen.width;
屏幕分辨率的高: window.screen.height;
--------------------------------------------------------------------
屏幕可用宽度:   window.screen.availWidth;
屏幕可用高度:   window.screen.availHeight;
-->


正在回答

1 回答

你可以看下这些属性不同浏览器的写法都是怎么写的,然后你把这些写法都写上,这样总会有浏览器会识别其中的一种写法。这是我理解的一种兼容

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

整理了scrollWidth offsetWidth clientWidth innerWidth,不同浏览器输出结果不一样,实际应用中怎么办?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信