document.documentElement.clientWidth

关于获取各种浏览器可见窗口大小的一点点研究

<script>
function getInfo()
{
var s = "";
s = " 网页可见区域宽:" document.body.clientWidth;
s = " 网页可见区域高:" document.body.clientHeight;
s = " 网页可见区域宽:" document.body.offsetWidth " (包括边线和滚动条的宽)";
s = " 网页可见区域高:" document.body.offsetHeight " (包括边线的宽)";
s = " 网页正文全文宽:" document.body.scrollWidth;
s = " 网页正文全文高:" document.body.scrollHeight;
s = " 网页被卷去的高(ff):" document.body.scrollTop;
s = " 网页被卷去的高(ie):" document.documentElement.scrollTop;
s = " 网页被卷去的左:" document.body.scrollLeft;
s = " 网页正文部分上:" window.screenTop;
s = " 网页正文部分左:" window.screenLeft;
s = " 屏幕分辨率的高:" window.screen.height;
s = " 屏幕分辨率的宽:" window.screen.width;
s = " 屏幕可用工作区高度:" window.screen.availHeight;
s = " 屏幕可用工作区宽度:" window.screen.availWidth;

s = " 你的屏幕设置是 " window.screen.colorDepth " 位彩色";
s = " 你的屏幕设置 " window.screen.deviceXDPI " 像素/英寸";
//alert (s);
}
getInfo();
</script>


在本地测试当中:
在IE、FireFox、Opera下都可以使用
document.body.clientWidth
document.body.clientHeight

即可获得,很简单,很方便。

而在公司项目当中:
Opera仍然使用
document.body.clientWidth
document.body.clientHeight


可是IE和FireFox则使用
document.documentElement.clientWidth
document.documentElement.clientHeight


原来是W3C的标准在作怪啊

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
如果在页面中添加这行标记的话 在IE中:
document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度


在FireFox中:
document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度


在Opera中:
document.body.clientWidth ==> 可见区域宽度
document.body.clientHeight ==> 可见区域高度
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)
document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)



而如果没有定义W3C的标准,则
IE为:
document.documentElement.clientWidth ==> 0
document.documentElement.clientHeight ==> 0


FireFox为:
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)

document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)

Opera为:
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)

document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)



本文转自:http://blog.sina.com.cn/s/blog_6f1f9ead0100n1f6.html

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>跳动的小球</title> <style type="text/css"> *{margin:0;padding:0;font-family:"Microsoft Yahei";} body{ overflow: hidden; } #box{ position: absolute; z-index: 1; top: 0; left: 0; width: 50px; height: 50px; background: radial-gradient(#ff0,#f00); border-radius: 50%; } .shadow{ position: absolute; width: 50px; height: 50px; background: pink; border-radius: 50%; transform: scale(1); opacity: 1; animation: hide .5s 1; animation-fill-mode: forwards; } @keyframes hide { from{transform: scale(1);opacity: 1} to{transform: scale(0);opacity: 0} } </style> </head> <body> <div id="box"></div> <script type="text/javascript"> (function(){ //获取节点 let oBox = document.getElementById("box"); //提前计算好最大值限制 let maxH = document.documentElement.clientHeight - 50, maxW = document.documentElement.clientWidth - 50; //监听resize,改变maxH,maxW window.onresize = function(){ maxH = document.documentElement.clientHeight - 50; maxW = document.documentElement.clientWidth - 50; }; //提前定义好,水平方向的速度 和 垂直方向的速度 let Vx = 20,Vy = 8; (function m(){ //先计算球即将要到达的位置 let left = oBox.offsetLeft + Vx, top = oBox.offsetTop + Vy; //做边界判断 if ( left >= maxW ){ left = maxW; Vx = -Vx; } if (top >= maxH){ top = maxH; Vy = -Vy; } if (left <= 0){ left = 0; Vx = -Vx; } if(top <= 0){ top = 0; Vy = -Vy; } //改变样式 oBox.style.left = left + 'px'; oBox.style.top = top + 'px'; //每次帧动画时创建一个相同大小的球 createShadow(left,top); requestAnimationFrame(m); })(); //阴影盒子的生产 function createShadow(left,top){ let div = document.createElement("div"); div.classList.add("shadow"); div.style.cssText = `left:${left}px;top:${top}px;`; document.body.appendChild(div); setTimeout(()=>{ document.body.removeChild(div); div = null; },1000) } })(); </script> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值