使用documentElement正確取得當前可見地區的大小,documentelement

來源:互聯網
上載者:User

使用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變數取得視窗大小。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.