使用documentElement正確取得當前可見地區的大小,documentelement
其他方法都不適用,只有這個documentElement才能正確取得當前可見地區的大小
var w = parseInt(document.documentElement.clientWidth);var h = parseInt(document.documentElement.clientHeight);
javascript怎獲得網頁可見地區的高度(不包括被捲軸捲去的內容) 就是你可以看到的網頁內容的高度
你可以參考下面這個函數,這個函數是擷取完整頁面尺寸的函數(即你說的瀏覽器能看到的地區,不包括被捲軸捲去的地區)
-----------------------------js代碼---------------------------------------------------------
<script>
function GetPageSize(){
var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else {
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) {
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) {
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}
if(xScroll < windowWidth){
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}
alert(GetPageSize());......餘下全文>>
JS動態擷取瀏覽器視窗大小的問題
它這個是產生了一個對象,這個對象有個叫w的屬性,用於存放視窗寬度,h就是存放高度。
這樣,返回後,就可以用類似getViewSize()[w]的方法調用寬度。
如果要能動態改變,可以這樣:
var size=getViewSize();
window.onresize=function(){size=getViewSize();}
這樣,無論何時,都可以通過size變數取得視窗大小。