javascript 中的各類width,height屬性整理

目標javascript

  1. 理清js及jquery的各類width和height

對象:css

  • Window對象表示瀏覽器中打開的窗口;window對象能夠省略。好比alert()、window.alert()。
  • Document對象是Window對象的一部分。那麼document.body 咱們能夠寫成window.document.body;瀏覽器的HTML文檔成爲Document對象。
  • window對象的location屬性引用的是Location對象,表示該窗口中當前顯示文檔的URL。
  • document的對象的location屬性也是引用了Location對象。(window.location === document.location;  //true)

與window相關的寬高有一下幾個:html

  1. window.innerWidth,經過字面意思咱們知道這是一個內部的寬度
  2. window.innerHeight,內部的高度
  3. window.outerWidth,外部的寬度
  4. window.outerHeight,外部的高度

    window.innerHeight和window.outHeightjava

  1. window.innerWidth和window.outWidthjquery

     

    掛靠在window下的寬高還有window.screen, window.screen包含有關用戶屏幕的信息。它包括:瀏覽器

    • window.screen.width
    • window.screen.height
    • window.screen.availHeight
    • window.screen.availWidth
    • window.screenTop
    • window.screenLeft

    圖解spa

    window相關寬高代碼演示

    演示代碼: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

     

    window相關寬高瀏覽器兼容問題

    innerHeight和outerHeight是不支持IE9如下版本的,而screen系列則不存在兼容問題。

    document下面client相關寬高介紹

    docment下有三類屬性:

    1. 與client相關的寬高
    2. 與offset相關的寬高
    3. 與scroll相關的寬高

    與client相關的寬高

    與client相關的寬高又有以下幾個屬性:

    • document.body.clientWidth
    • document.body.clientHeight
    • document.body.clientLeft
    • document.body.clientTop

    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相關寬高介紹

    與offset相關的寬高又有以下幾個屬性:

    • document.body.offsetWidth
    • document.body.offsetHeight
    • document.offsetLeft
    • document.offsetTop

    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相關寬高介紹

    與scroll相關的寬高屬性有以下幾個:

    • document.body.scrollWidth
    • document.body.scrollHeight
    • document.body.scrollLeft
    • document.body.scrollTop

    scrollWidth和scrollHeight
    document.body的scrollWidth和scrollHeight與div的scrollWidth和scrollHeight是有區別的。

    咱們先來看看document.body的scrollWidth和scrollHeight:

    1. 給定寬高小於瀏覽器窗口
      • scrollWidth一般是瀏覽器窗口的寬度
      • scrollHeight一般是瀏覽器窗口的高度
    2. 給定寬高大於瀏覽器窗口,且內容小於給定寬高
      • scrollWidth給定的寬度+其全部padding、margin和border
      • scrollHeight給定的高度+其全部的padding、margin和border
    3. 給定寬高大於瀏覽器窗口,且內容大於給定寬高
      • scrollWidth內容寬度+其全部的padding、margin和border
      • scrollHeight內容高度+其全部的padding、margin和border

    再來看看在某div中scrollWidth和scrollHeight:

      • 在無滾動軸的時候

     

    • scrollWidth==clientWidth=style.width+style.padding*2
    • 在有滾動軸的時候

    • scrollWidth==clientWidth=style.width+style.padding*2
    • 在有滾動軸的時候

            

scrollWidth==實際內容的寬度+padding2
scrollHeight==實際內容的高度+padding
2

 

 

ps.Event對象的5種座標

哪五種座標?

    1. clientX和clientY,相對於瀏覽器(但是區左上角0,0)的座標
    2. screenX和screenY,相對於設備屏幕左上角(0,0)的座標
    3. offsetX和offsetY,相對於事件源左上角(0,0)的座標
    4. pageX和pageY,相對於整個網頁左上角(0,0)的座標
    5. X和Y,原本是IE屬性,相對於用CSS動態定位的最內層包容元素
<!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>

 

相關文章
相關標籤/搜索