目標:javascript
對象:css
document.body
咱們能夠寫成window.document.body
;瀏覽器的HTML文檔成爲Document對象。window.location === document.location; //true)
與window相關的寬高有一下幾個:html
window.innerHeight和window.outHeightjava
window.innerWidth和window.outWidthjquery
掛靠在window下的寬高還有window.screen, window.screen包含有關用戶屏幕的信息。它包括:瀏覽器
圖解spa
演示代碼:3d
console.log("innerWidth=",innerWidth); console.log("innerHeight=",innerHeight); console.log("outerWidth=",outerWidth); console.log("outerHeight=",outerHeight);
Chrome瀏覽器下效果
代碼:code
console.info("screen.width=",screen.width); console.info("screen.height=",screen.height); console.info("screen.availWidth=",screen.availWidth); console.info("screen.availHeight=",screen.availHeight);
在Chrome瀏覽器下效果
htm
innerHeight和outerHeight是不支持IE9如下版本的,而screen系列則不存在兼容問題。
document下面client相關寬高介紹
docment下有三類屬性:
與client相關的寬高又有以下幾個屬性:
clientWidth和clientHeight 該屬性指的是元素的可視部分寬度和高度,即padding+contenr
。 若是沒有滾動條,即爲元素設定的高度和寬度。 若是出現滾動條,滾動條會遮蓋元素的寬高,那麼該屬性就是其原本寬高減去滾動條的寬高。
咱們來看以下代碼:
body{ border: 20px solid #000; margin: 10px; padding: 40px; background: #eee; height: 350px; width: 500px; overflow: scroll; } console.log(document.body.clientHeight); //430(padding*2+height) console.log(document.body.clientWidth); //580(padding*2+width)
與offset相關的寬高又有以下幾個屬性:
offsetWidth與offsetHeight 這一對屬性指的是元素的border+padding+content的寬度和高度。
該屬性和其內部的內容是否超出元素大小無關,只和原本設定的border以及width和height有關。 咱們仍是以body爲例:
body{ border: 20px solid #000; margin: 10px; padding: 40px; background: #eee; height: 350px; width: 500px; overflow: scroll; } console.log("offsetWidth=",document.body.offsetWidth); //620(width+padding*2+border*2) console.log("offsetHeight=",document.body.offsetHeight); //470(width+padding*2+border*2)
與scroll相關的寬高屬性有以下幾個:
scrollWidth和scrollHeight
document.body的scrollWidth和scrollHeight與div的scrollWidth和scrollHeight是有區別的。
咱們先來看看document.body的scrollWidth和scrollHeight:
再來看看在某div中scrollWidth和scrollHeight:
scrollWidth==實際內容的寬度+padding2
scrollHeight==實際內容的高度+padding2
ps.Event對象的5種座標
哪五種座標?
<!DOCTYPE html> <html> <head> <title>javascript height width test</title> <style type="text/css"> body { margin:0px;padding: 0px;width: 300px;border: 10px solid blue; } </style> </head> <body> </body> <script type="text/javascript"> document.write("innerHeight="+window.innerHeight);//瀏覽器可視高度 document.write("innerWidth="+window.innerWidth+"<br>");//瀏覽器可視寬度 document.write("outerHeight="+window.outerHeight);//瀏覽器實際高度 document.write("outerWidth="+window.outerWidth+"<br>");//瀏覽器實際寬度 document.write("clientHeight="+document.body.clientHeight);//content+padding*2 高度 document.write("clientWidth="+document.body.clientWidth+"<br>");//content+padding*2 寬度 //總結 //•假入無padding無滾動條,clientWidth=style.width //•假若有padding無滾動軸,clientWidth=style.width+style.padding*2 //•假若有padding有滾動,且滾動是顯示的,clientWidth=style.width+style.padding*2-滾動軸寬度 document.write("clientLeft="+document.body.clientLeft); document.write("clientTop="+document.body.clientTop+"<br>"); //素周圍邊框的厚度,若是不指定一個邊框或者不定位該元素,他的值就是0 document.write("offsetHeight="+document.body.offsetHeight);//content+padding*2+border*2 高度 document.write("offsetWidth="+document.body.offsetWidth);//content+padding*2+border*2 寬度 // 總結 //•假如無padding無滾動條無border: •offsetWidth=clientWidth=style.width //•假若有padding無滾動條有border: •offsetWidth=style.width+style.padding2+(border-width)2 //•offsetWidth=clientWidth+border寬度*2 //•假若有padding有滾動條,且滾動條是顯示的,有border: •offsetWidth=style.width+style.padding2+(border-width)2 //•offsetWidth=clientWidth+滾動條寬度+border寬度*2 </script> </html>