首頁  >  文章  >  web前端  >  js/jquery取得瀏覽器捲軸高度和寬度實作用法詳解

js/jquery取得瀏覽器捲軸高度和寬度實作用法詳解

伊谢尔伦
伊谢尔伦原創
2017-07-19 15:55:363293瀏覽

取得瀏覽器視窗的視覺區域高度和寬度,捲軸高度有需要的朋友可參考一下。

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() 
} 
} 
}

#另附最常用的取得整頁寬高的方法(需要jquery框架)

#
$(document).width() < $(&#39;body&#39;).width() ? $(document).width() : $(&#39;body&#39;).width(); 
$(document).height() < $(&#39;body&#39;).height() ? $(document).height() : $(&#39;body&#39;).height();
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