首頁  >  文章  >  web前端  >  js/jquery取得瀏覽器視窗視覺區域高度和寬度以及捲軸高度實作程式碼

js/jquery取得瀏覽器視窗視覺區域高度和寬度以及捲軸高度實作程式碼

高洛峰
高洛峰原創
2017-01-11 09:39:251637瀏覽

取得瀏覽器視窗的可視區域高度和寬度,捲軸高度有需要的朋友可參考一下。
IE中,瀏覽器顯示視窗大小只能以下取得: 程式碼如下複製程式碼 

document.body.offsetWidth 
document.body.offsetHeight

在宣告了DOCTYPE的瀏覽器中,可以用以下來取得瀏覽器顯示視窗大小: 程式碼如下複製程式碼

document.documentElement.clientWidth 
document.documentElement.clientHeight

IE, FF,Safari皆支援此方法,opera雖支援該屬性,但是返回的是頁面尺寸;
同時,除了IE以外的所有瀏覽器都將此資訊保存在window物件中,可用以下取得: 程式碼如下複製程式碼

window.innerWidth 
window.innerHeight

整個網頁尺寸一般取得方法 程式碼如下複製程式碼

document.body.scrollWidth 
document.body.scrollHeight

螢幕解析度高度一般取得方法 程式碼如下複製程式碼 

window.screen.height 
window.screen.width

總結實例

function getViewSizeWithoutScrollbar(){//不包含滚动条 
return { 
width : document.documentElement.clientWidth, 
height: document.documentElement.clientHeight 
} 
} 
function getViewSizeWithScrollbar(){//包含滚动条 
if(window.innerWidth){ 
return { 
width : window.innerWidth, 
height: window.innerHeight 
} 
}else if(document.documentElement.offsetWidth == document.documentElement.clientWidth){ 
return { 
width : document.documentElement.offsetWidth, 
height: document.documentElement.offsetHeight 
} 
}else{ 
return { 
width : document.documentElement.clientWidth + getScrollWith(), 
height: document.documentElement.clientHeight + getScrollWith() 
} 
} 
}

IE,FireFoxreee
總結一下實例

clientWidth = width + padding 
clientHeight = height + padding 
offsetWidth = width + padding + border 
offsetHeight = height + padding + border 
IE5.0/5.5: 
clientWidth = width - border 
clientHeight = height - border 
offsetWidth = width 
offsetHeight = height

IE,FireFox 差異如下:

總結一下實例

$(document).width() < $(&#39;body&#39;).width() ? $(document).width() : $(&#39;body&#39;).width(); 
$(document).height() < $(&#39;body&#39;).height() ? $(document).height() : $(&#39;body&#39;).height();
+: 
rrreee
另附個人最常用的取得整頁寬高的方法(需要jquery框架) 程式碼如下複製程式碼
rrreee
alert($(window).height()); //瀏覽器時下方視窗視覺區域高度
alert($(document).height()); //瀏覽器時下視窗文檔的高度
alert($(document.body).height());//瀏覽器時下視窗文檔body的高度
alert($(document.body).outerHeight(true));//瀏覽器時下視窗文檔body的總高度包括border padding margin
alert($(window).width()); //瀏覽器時下視窗可視區域寬度
alert($(document).width());//瀏覽器時下視窗文件對於象寬度
alert($(document.body).width());//瀏覽器時下視窗文件body的高度

alert($(document.body).outerWidth(true));//瀏覽器時下視窗文件body的總寬度包括border padding margin

alert($(document).scrollTop()); //取得滾動條到頂部的垂直高度🎜alert($(document).scrollLeft()); //取得捲軸到左邊的垂直寬度🎜🎜更多js/jquery取得瀏覽器視窗可視區域高度和寬度以及捲軸高度實作代碼相關文章請關注PHP中文網! 🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn