Home  >  Article  >  Web Front-end  >  Detailed explanation of usage of js/jquery to obtain browser scroll bar height and width

Detailed explanation of usage of js/jquery to obtain browser scroll bar height and width

伊谢尔伦
伊谢尔伦Original
2017-07-19 15:55:363357browse

Get the height and width of the visible area of ​​the browser window. Friends who need the height of the scroll bar can refer to it.

In IE, the browser display window size can only be obtained as follows: Copy the code as follows

document.body.offsetWidth 
document.body.offsetHeight

In a browser that declares DOCTYPE, you can use the following to obtain the browser display Window size: Copy the code as follows

document.documentElement.clientWidth 
document.documentElement.clientHeight

IE, FF, and Safari all support this method. Although opera supports this attribute, it returns the page size;
At the same time, all browsers except IE This information is saved in the window object and can be obtained with the following: The code is as follows Copy the code

window.innerWidth 
window.innerHeight

The general method of obtaining the size of the entire web page The code is as follows Copy the code

document.body.scrollWidth 
document.body.scrollHeight

The general method of obtaining the screen resolution height Code Copy the code as follows

window.screen.height 
window.screen.width

Summarize the example

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

Attached is the most commonly used method of obtaining the width and height of the entire page (requires jquery framework)

$(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()); //获取滚动条到左边的垂直宽度

The above is the detailed content of Detailed explanation of usage of js/jquery to obtain browser scroll bar height and width. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn