js/jquery獲取瀏覽器窗口可視區(qū)域高度和寬度以及滾動(dòng)條高度實(shí)現(xiàn)代碼
更新時(shí)間:2012年12月17日 17:09:59 作者:
在js使用過(guò)程中可能會(huì)根據(jù)要求獲取瀏覽器窗口的可視區(qū)域高度和寬度,滾動(dòng)條高度,于是本人搜集整理下,拿出來(lái)和大家分享,希望可以幫助你們
獲取瀏覽器窗口的可視區(qū)域高度和寬度,滾動(dòng)條高度有需要的朋友可參考一下。
IE中,瀏覽器顯示窗口大小只能以下獲?。?nbsp;代碼如下復(fù)制代碼
document.body.offsetWidth
document.body.offsetHeight
在聲明了DOCTYPE的瀏覽器中,可以用以下來(lái)獲取瀏覽器顯示窗口大?。?nbsp;代碼如下復(fù)制代碼
document.documentElement.clientWidth
document.documentElement.clientHeight
IE,F(xiàn)F,Safari皆支持該方法,opera雖支持該屬性,但是返回的是頁(yè)面尺寸;
同時(shí),除了IE以外的所有瀏覽器都將此信息保存在window對(duì)象中,可以用以下獲?。?nbsp;代碼如下復(fù)制代碼
window.innerWidth
window.innerHeight
整個(gè)網(wǎng)頁(yè)尺寸一般獲得方法 代碼如下復(fù)制代碼
document.body.scrollWidth
document.body.scrollHeight
屏幕分辨率高度一般獲得方法 代碼如下復(fù)制代碼
window.screen.height
window.screen.width
總結(jié)一下實(shí)例
function getViewSizeWithoutScrollbar(){//不包含滾動(dòng)條
return {
width : document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
function getViewSizeWithScrollbar(){//包含滾動(dòng)條
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,F(xiàn)ireFox 差異如下:
IE6.0、FF1.06+:
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
另附個(gè)人最常用的獲取整頁(yè)寬高的方法(需要jquery框架) 代碼如下復(fù)制代碼
$(document).width() < $('body').width() ? $(document).width() : $('body').width();
$(document).height() < $('body').height() ? $(document).height() : $('body').height();
alert($(window).height()); //瀏覽器時(shí)下窗口可視區(qū)域高度
alert($(document).height()); //瀏覽器時(shí)下窗口文檔的高度
alert($(document.body).height());//瀏覽器時(shí)下窗口文檔body的高度
alert($(document.body).outerHeight(true));//瀏覽器時(shí)下窗口文檔body的總高度 包括border padding margin
alert($(window).width()); //瀏覽器時(shí)下窗口可視區(qū)域?qū)挾?BR>alert($(document).width());//瀏覽器時(shí)下窗口文檔對(duì)于象寬度
alert($(document.body).width());//瀏覽器時(shí)下窗口文檔body的高度
alert($(document.body).outerWidth(true));//瀏覽器時(shí)下窗口文檔body的總寬度 包括border padding margin
alert($(document).scrollTop()); //獲取滾動(dòng)條到頂部的垂直高度
alert($(document).scrollLeft()); //獲取滾動(dòng)條到左邊的垂直寬度
IE中,瀏覽器顯示窗口大小只能以下獲?。?nbsp;代碼如下復(fù)制代碼
復(fù)制代碼 代碼如下:
document.body.offsetWidth
document.body.offsetHeight
在聲明了DOCTYPE的瀏覽器中,可以用以下來(lái)獲取瀏覽器顯示窗口大?。?nbsp;代碼如下復(fù)制代碼
復(fù)制代碼 代碼如下:
document.documentElement.clientWidth
document.documentElement.clientHeight
IE,F(xiàn)F,Safari皆支持該方法,opera雖支持該屬性,但是返回的是頁(yè)面尺寸;
同時(shí),除了IE以外的所有瀏覽器都將此信息保存在window對(duì)象中,可以用以下獲?。?nbsp;代碼如下復(fù)制代碼
復(fù)制代碼 代碼如下:
window.innerWidth
window.innerHeight
整個(gè)網(wǎng)頁(yè)尺寸一般獲得方法 代碼如下復(fù)制代碼
復(fù)制代碼 代碼如下:
document.body.scrollWidth
document.body.scrollHeight
屏幕分辨率高度一般獲得方法 代碼如下復(fù)制代碼
復(fù)制代碼 代碼如下:
window.screen.height
window.screen.width
總結(jié)一下實(shí)例
復(fù)制代碼 代碼如下:
function getViewSizeWithoutScrollbar(){//不包含滾動(dòng)條
return {
width : document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
function getViewSizeWithScrollbar(){//包含滾動(dòng)條
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,F(xiàn)ireFox 差異如下:
IE6.0、FF1.06+:
復(fù)制代碼 代碼如下:
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
另附個(gè)人最常用的獲取整頁(yè)寬高的方法(需要jquery框架) 代碼如下復(fù)制代碼
復(fù)制代碼 代碼如下:
$(document).width() < $('body').width() ? $(document).width() : $('body').width();
$(document).height() < $('body').height() ? $(document).height() : $('body').height();
alert($(window).height()); //瀏覽器時(shí)下窗口可視區(qū)域高度
alert($(document).height()); //瀏覽器時(shí)下窗口文檔的高度
alert($(document.body).height());//瀏覽器時(shí)下窗口文檔body的高度
alert($(document.body).outerHeight(true));//瀏覽器時(shí)下窗口文檔body的總高度 包括border padding margin
alert($(window).width()); //瀏覽器時(shí)下窗口可視區(qū)域?qū)挾?BR>alert($(document).width());//瀏覽器時(shí)下窗口文檔對(duì)于象寬度
alert($(document.body).width());//瀏覽器時(shí)下窗口文檔body的高度
alert($(document.body).outerWidth(true));//瀏覽器時(shí)下窗口文檔body的總寬度 包括border padding margin
alert($(document).scrollTop()); //獲取滾動(dòng)條到頂部的垂直高度
alert($(document).scrollLeft()); //獲取滾動(dòng)條到左邊的垂直寬度
相關(guān)文章
jQuery動(dòng)態(tài)設(shè)置form表單的enctype值(實(shí)現(xiàn)代碼)
本篇文章是對(duì)在jQuery中動(dòng)態(tài)設(shè)置form表單的enctype值的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07jQuery創(chuàng)建DOM元素實(shí)例解析
這篇文章主要介紹了jQuery創(chuàng)建DOM元素的方法,實(shí)例分析了jQuery使用$直接創(chuàng)建DOM元素的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01修改或擴(kuò)展jQuery原生方法的代碼實(shí)例
這篇文章主要介紹了修改或擴(kuò)展jQuery原生方法的代碼實(shí)例,本文用一個(gè)擴(kuò)展jquery原生方法val的例子,講解了如何對(duì)jquery原生方法修改或擴(kuò)展,需要的朋友可以參考下2015-01-01基于jquery實(shí)現(xiàn)的一個(gè)選擇中國(guó)大學(xué)的彈框 (數(shù)據(jù)、步驟、代碼)
基于jquery實(shí)現(xiàn)的一個(gè)選擇中國(guó)大學(xué)的彈框,需要的朋友可以參考下2012-07-07jquery控制select的text/value值為選中狀態(tài)
這篇文章主要對(duì)jquery控制select的text/value值為選中狀態(tài)做下總結(jié),省的每次使用都要到網(wǎng)上翻下2014-06-06jQuery實(shí)現(xiàn)的tab標(biāo)簽切換效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的tab標(biāo)簽切換效果,結(jié)合實(shí)例形式分析了jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)變換頁(yè)面元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-09-09