整理了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; -->