純Javascript獲取各類屏幕的寬度和高度

讀取基本寬高值:javascript

網頁可見區域寬: document.body.clientWidth
網頁可見區域高: document.body.clientHeight
網頁可見區域寬: document.body.offsetWidth (包括邊線的寬)
網頁可見區域高: document.body.offsetHeight (包括邊線的高)
網頁正文全文寬: document.body.scrollWidth
網頁正文全文高: document.body.scrollHeight
網頁被捲去的高: document.body.scrollTop
網頁被捲去的左: document.body.scrollLeft
網頁正文部分上: window.screenTop
網頁正文部分左: window.screenLeft
屏幕分辨率的高: window.screen.height
屏幕分辨率的寬: window.screen.width
屏幕可用工做區高度: window.screen.availHeight
屏幕可用工做區寬度: window.screen.availWidth

以上值不能徹底讀取瀏覽器窗口的實際尺寸,尤爲是高度,如下是更準確的方案:php

function findDimensions() { //函數:獲取尺寸
  var winWidth = 0, winHeight = 0;
  // 獲取窗口寬度
  if (window.innerWidth)
    winWidth = window.innerWidth;
  else if ((document.body) && (document.body.clientWidth))
    winWidth = document.body.clientWidth;
  // 獲取窗口高度
  if (window.innerHeight)
    winHeight = window.innerHeight;
  else if ((document.body) && (document.body.clientHeight))
    winHeight = document.body.clientHeight;

  // 經過深刻Document內部對body進行檢測,獲取窗口大小
  if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
    winHeight = document.documentElement.clientHeight;
    winWidth = document.documentElement.clientWidth;
  }
  // 結果輸出至浮動框
  document.getElementById("info").innerHTML = winWidth + " " + winHeight;
}

參考來源:html

相關文章
相關標籤/搜索